The navigation property on the "one" side of the relationship would be a collection that could hold multiple instances of the entity on the "many"side of the relationship. It would not be a primary key. The navigation property on the entity on the "many"
side would hold an instance of the entity on the "one" side. That would also not be a primary key. For more explanation, see the tutorials http://asp.net/entity-framework/tutorials
Thanks for that I am inserting a record into the many table and its coming up with an error regarding the navigation property, all the fields are filled in correct but I cannot figure out what i need to do to get it to work, I thought it might be that my
Foreign key wasn't the primary key but...
If I remove the navigation property it works fine. and the error is in the 1 table which I am not even using.
The foreign key in the many table should hold the value of a primary key in the one table. If you're using Database First you can find an example in the first tutorial series at the URL I mentioned; if you're using Code First, see the third series.
I'm not aware of another way to establish a one-many relationship in the database. You could make it a many to many relationship in which one side only happens to have one element though it could have more; in that case the foreign key in the many table
would contain part of the primary key of an association table but would not have the primary key of the one table.
public class oneTable
{
[Key]
public int oneTableID { get; set; }
public string oneTableName { get; set; }
public string oneTableGroup { get; set; }
public string Status { get; set; }// this is use to create the history
public virtual ICollection<manyTable> manyTable { get; set; }
}
public class ManyTable
{
[Key]
public int ManyTableID { get; set; }
public string ManyTableName { get; set; }
public string ManyTableGroup { get; set; }
public string oneTableGroup { get; set; }
public string Status { get; set; }
[ForeignKey("ManyTableGroup")]
public virtual oneTable oneTable { get; set; }
}
These are the models, the idea is I want to create a history, there could be 10 records which are the same but the status for the old records is "history" and the status for the current records is "active", then they all have the same group which creates
the history, however I want to be able to use lazy loading and intellinse so really would like the navigation properties to work. so link to the right record would be the oneTableGroup and status Active not necessaily the primary id.
I could get the primary ID to work but it would mean I would have to update more records and waste more space everytime someone update one small section.
Looks to me like you need a oneTableGroup table between oneTable and manyTable - many to one from oneTable to oneTableGroup, one to many from oneTableGroup to manyTable.
EnenDaveyBoy
Participant
1466 Points
1157 Posts
Entity Framwork / Navigation Property / SQL issue
Jul 12, 2011 04:10 PM|LINK
Hi
If I setup a one to many relationship with Navigation properties, does the navigation property have to be primary key?
tdykstra
Contributor
4523 Points
630 Posts
Microsoft
Moderator
Re: Entity Framwork / Navigation Property / SQL issue
Jul 12, 2011 04:34 PM|LINK
The navigation property on the "one" side of the relationship would be a collection that could hold multiple instances of the entity on the "many"side of the relationship. It would not be a primary key. The navigation property on the entity on the "many" side would hold an instance of the entity on the "one" side. That would also not be a primary key. For more explanation, see the tutorials http://asp.net/entity-framework/tutorials
EnenDaveyBoy
Participant
1466 Points
1157 Posts
Re: Entity Framwork / Navigation Property / SQL issue
Jul 12, 2011 05:38 PM|LINK
Thanks for that I am inserting a record into the many table and its coming up with an error regarding the navigation property, all the fields are filled in correct but I cannot figure out what i need to do to get it to work, I thought it might be that my Foreign key wasn't the primary key but...
If I remove the navigation property it works fine. and the error is in the 1 table which I am not even using.
tdykstra
Contributor
4523 Points
630 Posts
Microsoft
Moderator
Re: Entity Framwork / Navigation Property / SQL issue
Jul 12, 2011 05:58 PM|LINK
The foreign key in the many table should hold the value of a primary key in the one table. If you're using Database First you can find an example in the first tutorial series at the URL I mentioned; if you're using Code First, see the third series.
EnenDaveyBoy
Participant
1466 Points
1157 Posts
Re: Entity Framwork / Navigation Property / SQL issue
Jul 12, 2011 07:27 PM|LINK
Yes thats my issue I don't want the foreign key in the many table to be the primary key in the one table, and I am using code first.
EnenDaveyBoy
Participant
1466 Points
1157 Posts
Re: Entity Framwork / Navigation Property / SQL issue
Jul 12, 2011 07:29 PM|LINK
I have also done the tutorial in the link and although its good it doesn't really explain the if's and buts, or why's
tdykstra
Contributor
4523 Points
630 Posts
Microsoft
Moderator
Re: Entity Framwork / Navigation Property / SQL issue
Jul 12, 2011 07:39 PM|LINK
I'm not aware of another way to establish a one-many relationship in the database. You could make it a many to many relationship in which one side only happens to have one element though it could have more; in that case the foreign key in the many table would contain part of the primary key of an association table but would not have the primary key of the one table.
EnenDaveyBoy
Participant
1466 Points
1157 Posts
Re: Entity Framwork / Navigation Property / SQL issue
Jul 12, 2011 07:41 PM|LINK
public class oneTable
{
[Key]
public int oneTableID { get; set; }
public string oneTableName { get; set; }
public string oneTableGroup { get; set; }
public string Status { get; set; }// this is use to create the history
public virtual ICollection<manyTable> manyTable { get; set; }
}
public class ManyTable
{
[Key]
public int ManyTableID { get; set; }
public string ManyTableName { get; set; }
public string ManyTableGroup { get; set; }
public string oneTableGroup { get; set; }
public string Status { get; set; }
[ForeignKey("ManyTableGroup")]
public virtual oneTable oneTable { get; set; }
}
These are the models, the idea is I want to create a history, there could be 10 records which are the same but the status for the old records is "history" and the status for the current records is "active", then they all have the same group which creates the history, however I want to be able to use lazy loading and intellinse so really would like the navigation properties to work. so link to the right record would be the oneTableGroup and status Active not necessaily the primary id.
I could get the primary ID to work but it would mean I would have to update more records and waste more space everytime someone update one small section.
tdykstra
Contributor
4523 Points
630 Posts
Microsoft
Moderator
Re: Entity Framwork / Navigation Property / SQL issue
Jul 12, 2011 10:04 PM|LINK
Looks to me like you need a oneTableGroup table between oneTable and manyTable - many to one from oneTable to oneTableGroup, one to many from oneTableGroup to manyTable.
EnenDaveyBoy
Participant
1466 Points
1157 Posts
Re: Entity Framwork / Navigation Property / SQL issue
Jul 12, 2011 10:10 PM|LINK
So I need an icollection in both which will give me an assosiation table? (will that compromised the speed at all?)
I have to use this in alot of relationships, infact in almost every relationship between tables.
Or is there a better way to setup a history, the setup explained earlier was going to be used in all models