You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have the following data-model in Entity Framework 6.1.3:
using System.Data.Entity;publicclassStudent{publicintId{get;set;}publicvirtualContactContact{get;set;}}publicclassContact{publicintId{get;set;}publicvirtualStudentStudent{get;set;}}publicclassMyContext:DbContext{protectedoverridevoidOnModelCreating(DbModelBuilderbuilder){
builder.Entity<Contact>().HasOptional(x => x.Student).WithOptionalDependent(x => x.Contact).WillCascadeOnDelete(true);}}publicstaticclassProgram{privatestaticvoidMain(){
Database.SetInitializer(newDropCreateDatabaseAlways<MyContext>());using(varcontext=new MyContext())
context.Database.Initialize(force:true);}}
When I launch this code, I get exactly the right table structure I am aiming for:
dbo.Contacts
Id (PK)
Student_Id (FK, NULL, CASCADE ON DELETE)
dbo.Students
Id (PK)
However, now I would like to add the Student_Id property to be available in the Contact entity. So I can read the Student_Id without needing to join the other table through .Student.Id navigation.
If I add the property to the Contact entity, I end up either with two columns Student_Id and Student_Id1, or I end up with an error message saying Each property name in a type must be unique..
The column is already in the database, all I need is to have it in the entity as well, why is it so much trouble? Is there a solution?
The text was updated successfully, but these errors were encountered:
Unfortunately this is a limitation of EF6. You can not have a foreign key property in a one-to-one relationship, unless it is also the primary key property. This is essentially because EF6 doesn't support alternate keys/unique indexes, so you can't enforce that a non-primary key property is unique. The fact that you can do it when the foreign key property isn't in the entity is a bit of a quirk... but obviously not something we would remove 😄.
BTW alternate keys (and therefore this scenario) is supported in EF Core.
EF Team Triage: This issue is not something that our team is planning to address in the EF6.x code base. This does not mean that we would not consider a community contribution to address this issue.
Moving forwards, our team will be fixing bugs, implementing small improvements, and accepting community contributions to the EF6.x code base. Larger feature work and innovation will happen in the EF Core code base (https://github.com/aspnet/EntityFramework).
Closing an issue in the EF6.x project does not exclude us addressing it in EF Core. In fact, a number of popular feature requests for EF have already been implemented in EF Core (alternate keys, batching in SaveChanges, etc.).
BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.
I have the following data-model in Entity Framework 6.1.3:
When I launch this code, I get exactly the right table structure I am aiming for:
However, now I would like to add the
Student_Id
property to be available in theContact
entity. So I can read theStudent_Id
without needing to join the other table through.Student.Id
navigation.If I add the property to the
Contact
entity, I end up either with two columnsStudent_Id
andStudent_Id1
, or I end up with an error message sayingEach property name in a type must be unique.
.The column is already in the database, all I need is to have it in the entity as well, why is it so much trouble? Is there a solution?
The text was updated successfully, but these errors were encountered: