Invalid ColumnName, but ColumnName is correct (EFC) - entity-framework-core

So i'm trying to learn Design First EFC, but I have a problem getting Entities to work that have a many relationship with another Entity. I get this error message:SqlException: Invalid column name 'GuestListId'.
For experimentation I changed to the table into a one relationship. That works perfectly
This works:
public class GuestList
{
public Guid GuestListId { get; set; }
public Guest? Guest { get; set; }
}
However, when I want to have a many relationship, it does not work and fires the following errorcode: SqlException: Invalid column name 'GuestListId'.
This somehow does not work:
public class GuestList
{
public Guid GuestListId { get; set; }
public ICollection<Guest>? Guest { get; set; }
}
So I checked the GuestList table in SSMS, but that is identical to the entity in the sourcecode. I also updated the table but I still get the same problem. Does anyone have an idea what the problem is?
EDIT: So after a suggestion by someone, I added a GuestList property in Guest. But that still gives the same error:
public class Guest
{
public Guid GuestId { get; set; }
public string Name { get; set; }
public GuestList GuestList { get; set; }
}
So this is a picture of the sourcecode I have now that tries to insert data into the database.
Sourcecode
When I check the database, is see that GuestId value is not being assigned in the GuestList. But in the guest table I see that the GuestListid is being assigned:
Database Guest Table
Database GuestList Table
EDIT: I got it working now. As suggested, I migrated and updated my database. some tables have changed now. GuestList has only one column now and Guest has an extra column called GuestListId. See the pictures below:
enter image description here
enter image description here

Related

EF creating an unwanted field in database

I've hit a snag while building a .net mvc site. I have 2 related objects and am struggling with properly linking them. Specifically:
public class Address
{
public int AddressId { get; set; }
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostCode { get; set; }
[ForeignKey("AddressCategory")] // <-- EF adds field to below object's table
public int AddressCategoryId { get; set; }
public virtual AddressCategory AddressCategory { get; set; }
}
public class AddressCategory
{
public int AddressCategoryId { get; set; }
public string Description { get; set; }
}
Adding the [ForeignKey] data annotation to the Address object results in EF adding an Address_AddressId column to the AddressCategory table, which I don't want (or need) to happen.
I've tried to omit the ForeignKey attribute, but then I run into other errors because .net can't link the tables (e.g. Unknown column 'Extent1.AddressId' in 'field list'). Additionally, I wouldn't be able to use:
var addresses = db.Addresses.Include(l => l.AddressCategory);
Is there any way to link the 2 tables without EF adding an additional column to the AddressCategory table?
Thank you to #cloudikka for responding. After much trial-and-error I seem to have gotten it to work simply by omitting any ForeignKey reference from either object. I let EF rebuild the database and perform all scaffolding (CRUD forms) and they have been created perfectly.
My take-away is that foreign key attributes should be used for parent-child relationships, but not for look-up tables. I clearly have much to learn about asp.net mvc!

Entity framework "Field required" Error but works fine in debuuging

I have got a strange error in entityframework when I am trying to update an entity which uses some virtual (lazy load) properties. I retrieve the entity from the database and change a one to one property in that then try to update it again.The exception for some of the virtual properties is The field is required while others don't have this error.
What makes everything even more strange is that when I try to inspect the entity in debug mode the code just works fine and I don't face any errors at all.
Has anyone else experienced such behavior?
Thanks
Here is what code looks like:
public class IndependenceCheck : ModelBase
{
[Key]
public int IndependenceCheckID { get; set; }
public int PrincipalCompanyID { get; set; }
[Required]
[ForeignKey("PrincipalCompanyID")]
public virtual Company PrincipalCompany { get; set; }
[ForeignKey("OrganizationAddressID")]
[Required]
public virtual Address OrganizationAddress { get; set; }
}
There are much more fields in model, while I try to update no errors for PrincipalCompany occurs but for OrganizationAddress I got the required field error.

EF Code First: Does not update foreign key constraint name when property is changed

I probably found a bug... at least it's annoying to me so I would appreciate if someone confirms this:
public int PriorityConfigurationSettingId { get; set; }
[ForeignKey("PriorityConfigurationSettingId")]
public virtual PriorityConfigurationSetting
PriorityConfigurationSetting{get;set; }
to
public int PriorityConfigurationSettingId { get; set; }
[ForeignKey("PriorityConfigurationSettingId")]
public virtual LookUp PriorityConfigurationSetting { get; set; }
When I change the name of a foreign key entity name from PriorityConfigurationSetting to LookUp. After done the model changes i try to get the script through EF Code Fist Migration, I didn't get any changes.

EF Code First Table Splitting issue when used with many-to-many relationships

I am trying to use "Table Splitting" in EF Code First and it works fine when I use one of the entities relationships of type one-to-many, but as soon as I use it in a many-to-many relationship I start getting this error:
(27,6) : error 3018: Problem in mapping fragments starting at line
27:Foreign key constraint 'Itinerary_Addresses_Target' from table
ItineraryAddress (Address_Id) to table User (Id): The columns of table
ItineraryAddress are mapped to AssociationSet Itinerary_Addresses's
End Itinerary_Addresses_Target but the key columns of table User are
not mapped to the keys of the EntitySet Addresses corresponding to
this End.
Here is the code (https://github.com/jorgef/tablesplitting):
Table Splitting
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public Address Address { get; set; }
}
public class Address
{
public int Id { get; set; }
public string Street { get; set; }
public User User { get; set; }
}
modelBuilder.Entity<User>().ToTable("Users");
modelBuilder.Entity<Address>().ToTable("Users");
modelBuilder.Entity<User>().HasRequired(u => u.Address).WithRequiredPrincipal(a => a.User);
One-To-Many Relationship
public class Itinerary
{
public int Id { get; set; }
public ICollection<Address> Addresses { get; set; }
}
With the previous code, everything works like a charm, the problem is when introducing a many-to-many relationship
Many-To-Many Relationship
public class Address
{
...
public ICollection<Itinerary> Itineraries { get; set; }
}
After adding that relationship, the app raises the mentioned exception on runtime. I managed to save to disk the generated edmx just in case that helps, here is the link: https://github.com/jorgef/tablesplitting/blob/master/TableSplitting/SavedModel.edmx
If somebody wants to play with the two versions of the app, the one working and the one not working, I have two different commits:
Table splitting working with one to many relationship
Table splitting not working with many to many relationship
Any ideas or thougths are appreciated.
Many thanks!
In case anyone else has encountered this issue:
This was a bug in Entity Framework. The bug has since been fixed, and upgrading to EF6 will resolve the issue. See the following discussion for details:
https://entityframework.codeplex.com/workitem/1385
A related bug with table splitting validation has also been discovered and fixed, planned to be released with EF6.1.0:
https://entityframework.codeplex.com/workitem/1611

IsInRole - Error Invalid column name 'UserId'

This is an MVC4 project using SimpleMembership with EntityFramework First approach.
I am getting 2 errors when I try to use User.IsInRole("Admin").
1. Invalid column name "UserId".
2. Invalid column name 'RoleId'.
I suspect the reason is because I have a user class and respective table defined where the primary key field is called "Id" instead of "UserId" (which is the default in MVC4 template). This project is near its completion so I dont want to revert to using UserId unless I really really have to.
public class User
{
public long Id { get; set; }
public string UserName { get; set; }
public string FullName { get; set; }
public string Email { get; set; }
public string Intro { get; set; }
public string Locale { get; set; }
}
The primary key columns on the other simple membership tables are the defaults as follows
Users : Id
webpages_Membership : UserId
webpages_UsersInRoles : Users_Id
webpages_Roles : RoleId
All other membership functionality works currently fine. ie. Login, Register, OAuth Login etc.
If anyone can shed any light on this please do as it killing me at the moment!
Thanks