have primary key set up but says it is not defined? - c#

before you link to another post, i am using migrations and all that i can find don't use the way i have to do it.
first this is homework,
second here is a link to a google drive with the "full" project project fill stuff that was given to help, and a word doc with specifications(but the last one is not as important)
so on to the problem i am tring to do my homework the package manager says this "The entity type 'Categories' requires a primary key to be defined. If you intended to use a keyless entity type call 'HasNoKey()'." almost no matter what i do. i can get it to stop but then the database is not actual made at all.
my teacher is not responing to my emails and the other students in my class that have responed have the same issue, and this is the very beginning of the project and for the last few days i have been stuck here so please help.
i will add edit as questions get asked, i am panicking on getting this done in time so i don't know what is important or not.

First of all, double check you actually set PrimaryKey on your Database table. Then use [Key] attribute from System.ComponentModel.DataAnnotations namespace.
public class Categories
{
[Key]
public string CategoryId { get; set; }
public string Name { get; set; }
}
And also, next time, make sure your ID fields are either numeric or Guid and auto generated by database identity specification.. string Ids are slow for querying db records and to index tables.

Must tell model CategoryId is key. Best to use int as type.
[Key]
public int CategoryId { get; set; }

Related

Entity Framework Core: Persisting One-To-Many without existing foreign Entity

Having recently moved to C#/.Net Core from other languages, I got stuck on a problem with EF Core that I couldn't figure out from the documentation and hope you may be able to help. In a way this is related but not identical to my previous question .Net Core [FromBody] JArray to ICollection
My database holds a number of appointments that are rendered on a Syncfusion schedule. Attendees can be invited to these appointments. To facilitate that, a list of users is displayed in the editor and a JSON array of guids is transmitted with any insert or update action.
The User entity itself is not available within the scope of the application, so I'd like to persist only their Guids for each appointment. I had foreseen this structure:
Appointments (Start, End, ..., ´ICollection Attendees´)
Attendee would simply consist of AppointmentsId and Guid -
Since any one Guid can only attend each Appointment once, a composite Key made up of these two attributes appeared to be useful.
Any appointment can have none, one or many associated Guids.
In Code, I have this (abbreviated):
public class Appointment
{
public int Id { get; set; }
public DateTime StartTime { get; set; }
public ICollection<Attendee> AttendeeList { get; set; }
[NotMapped]
public List<Guid> PostedAttendeeList { get; set; } // Contains a list of Guids after an Insert/Update POST action from [FromBody]
}
Attendee would simply be made up of the Appointment Id and a Guid of a user.
public class Attendee
{
public Guid Id { get; set; }
public int AppointmentId { get; set; }
}
Attendee's configuration is this:
public class AttendeeConfiguration : IEntityTypeConfiguration<Attendee>
{
public void Configure(EntityTypeBuilder<Attendee> builder)
{
builder.HasKey(x => new { x.Id, x.AppointmentId });
}
}
After receiving a POST from the schedule, ´PostedAttendeeList´ may be empty or contain one or more Guids.
If it is an existing Appointment, ´AttendeeList´ may be empty or contain one or more Guids.
I'm wondering about a few things:
a) is there a better way to go about persisting this kind of data? I've tried to understand Owned Entity Types but failed to see if that would help me here.
b) if this is indeed an ok way to handle this, how can I make sure that ´AttendeeList´ is identical to ´PostedAttendeeList´ after processing, so that all new entries are added and those not present in ´PostedAttendeeList´ are removed through EF Core?
I'm especially confused about whom's responsibility it is to maintain ´AppointmentId´ - I wanted to keep the property visible but I understand that EF would fill that in when operating within the base property? Ie. that within the class ´Appointment´, a ´AttendeeList.Add(new Attendee() { Id = "1234-abcd-..." }´ would automagically fill in the AppointmentId upon saving?
If you read until here and are confused, please take a moment to remember when you started programming - I'm thoroughly confused and unable to come up with a better question. Even if you cannot help out with an answer, maybe you could help me make the question better. Thank you all very much! Any comment with suggestions will result in an update to my question in order to improve it.

Cannot insert explicit value for identity column in table when IDENTITY_INSERT is set to OFF

Alright, this question has been asked few times. But I can't get to find reason why i am getting this reason in my case. Here is how tables and code is set up.
public class ObjectA{
public int Id;
public virtual ICollection<ObjectB> Bs;
}
public class ObjectB{
public int Id;
public virtual ICollection<ObjectA> As;
}
POCO Mapping on B:
HasMany(As).WithMany(Bs).Map(ToTable("ObjectA_ObjectB_Mapping"))
There are 3 tables:
ObjectA
Id (PK, Int) - AutoIncrement
ObjectB
Id(PK, Int) - AutoIncrement
ObjectA_ObjectB_Mapping
ObjectA_Id
ObjectB_Id
Here is new object is being inserted:
var a = new ObjectA(){}; --> This record already exist in table
var b = new ObjectB(){
A = a; --> Just read only copy
};
When this insert is attempted then I get error about IDENTITY_INSERT is set to OFF.
When I look at SQL generated by EF, I can see that it is trying to insert value in field Id of ObjectB. It seems that there is something that wants to explictly wants to set this Id value. Something I am missing about how many to many relation is set??
Above code is just a very simplified pseudo representatiion, so you can skip comments about telling names, style etc.
Thanks
The easiest fix is to set IDENTITY_INSERT to ON, but I suspect that's not the root problem here, and I wouldn't recommend doing that for obvious reasons, it's just bad practice.
It sounds like EF (or you) is trying to manage the ID's itself, rather than allowing SQL server to. I don't know why, I suspect your actual objects have things going on, but you can also attach the DatabaseGeneratedAttribute (in the System.ComponentModel.DataAnnotations.Schema namespace) to the Id column on your model to force EF to not create an IDENTITY(1, 1) column (or AutoIncrement, depending on DBMS), this creates the requirement for you, the developer to manage the Id column on that model entirely. (Before you can do any SQL operations you have to make sure Id is set properly, including .Add.)
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }
This will remove the IDENTITY(1, 1) constraint (or the AutoIncrement, whatever it is). You'll have to run a migration for this to take effect, but once it does you should not get this error anymore. You just have to be very careful to make sure you set Id on everything before calling DatabaseContext.SaveChanges() or DatabaseContext.SaveChangesAsync(). (Whichever you are using.)
You should also explicitly define the KeyAttribute (from System.ComponentModel.DataAnnotations) on your Id columns.
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }

ASP .NET database-first picklist field

I want to create database table field which needs to have certain value.
For example field called season can have value:
spring, summer, autumn or winter.
public int id { get; set; }
public string season { get; set; }
public int yearNumber { get; set; }
public string season {get; set; }?
Could anyone help me? I'am aware that this question might be a little noobish but I am a totall newbie in ASP .NET not talking about web development...
Create a new table called Season.
Reference this table with Foreign-Key relationship.
This is a one-to-many relationship. Each of your Seasons can have many records in the other table.
In this case you should likely create another table that contains the 4 seasons. Often (but not always) you'll want to create an integer Id for each of those seasons, but you could just use the season's name as the Id (a varchar). You would reference this id as a Foreign Key. This is known as a Foreign Key relationship in Relational Databases (RDMS's), and it's used to Normalize the database. Normalization is what prevents you from repeating data (among other things) over and over, which can sometimes lead to data corruption It can be a little daunting if you're new to it, but it's a good thing to know.
http://blog.sqlauthority.com/2008/09/08/sql-server-%E2%80%93-2008-creating-primary-key-foreign-key-and-default-constraint/

Primary Key Does not autoincrement Entity Framework in C#

I have three small model classes and although two of them does works one is not and I can't figure out why. I found several solutions but neither of them helped me. First of all, code first approach was used in the project.
So, the main problem is that the PK in the Coupon class is not set to autoincrement value. I refer to the tables from Server Explorer and see PK's properties. Realized that other two classes PK's properties are set as Is Identity to True and Identity Increment = 1 whereas in the Coupon's PK's property they are set as Is Identity to False and Identity Increment to 0.
I think the problem is somewhere there and below you can find the small model class I am having trouble with.
public class Coupon
{
public Coupon()
{
footballMatches = new List<FootballMatch>();
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CouponId { get; set; }
public int UserId { get; set; }
public virtual List<FootballMatch> footballMatches { get; set; }
}
Ask me more if you need further information.
Thanks !
Not expected that, but I found the solution in such a silly way. The solution to my problem was just re-adding the key. At first, I did not agree with marc_s because I also tried re-adding before. Apparently, I did in a wrong way when I firstly tried re-adding.
What I made wrong is converting int to string then converting back to int while I did not touch the name of the instance, which is CouponId. That did not work. However, and sadly, I also had to change the name of the instance as well to make it work. I changed the line public int CouponId to public string CouponName then convert back to the actual name. Now it shows the expected behaviour.
Briefly, the simple solution is to change the key both variable and name and alter them to their actual names again. That simply.
By the way, while doing them please do not forget to update your migrations between each step. So the workflow is like Change->Update->Change Back->Update.
Hope that can help others who went through the same trouble.

Choosing name of column in entity framework code-first migrations

I have seen similar questions to this; but none quite the same; and none have helped me. I want the migration to use a different name for the column than I have for the property in my class. On built-in types, I am able to do this with [Column("newName")]. However, this doesn't work when I want a foreign key to another class.
In other words, this works just fine:
[Column("NameInDB")]
public string NameInCode { get; set; }
But this doesn't work at all:
[Column("Employee_Id")]
public virtual Employee Owner { get; set; }
In the second case, the migration still creates the column as Owner_Id; it completely ignores the Column annotation. Is there somewhere that it says that the Column annotation only works for built-in types? I couldn't find anything about that.
I know that it is possible to use the [ForeignKey] annotation to do this, but if I do, I have to have an extra property in my code that I don't want:
[ForeignKey("Employee_Id")]
public virtual Employee Owner { get; set; }
public int Employee_Id { get; set; }
I don't want to do that because the Employee_Id property is redundant in that case; I'd rather just use the Owner property. Is there a way around this, or a good reason that [Column] seems to be ignored?
public virtual Employee Owner { get; set; } is not mapped to DB, it is not represented in database. It populated by Entity Framework automatically for your convenience. Employee_Id - this is what is stored in DB. Entity Framework uses Employee_Id field to create Owner object. So, Emplyee_Id is not redundant in that case, it is mandatory and it has physical representation in DB, but Owner field is logical part of the class, managed by Entity Framework and not have it's column in DB. Makes sense?

Categories

Resources