Saving data for related Tables with foreign key - Entity Framework
I am having trouble saving my entities to the database using EntityFramework. I have to tables, Player and Address. The Player table has a reference to the Ad开发者_如何转开发dress table by means of an AddressId foreign key relationship. So Player.AddressId points to Address.AddressId.
Now when calling the following code, I get an error that says: "A dependent property in a ReferentialConstraint is mapped to a storage-generated column"
public override int CreatePlayer(BizObjects.Player player)
{
var entity = new EntityFramework.Player();
entity.PlayerId = player.PlayerId;
entity.FirstName = player.FirstName;
entity.LastName = player.LastName;
entity.Gender = player.Gender;
entity.BirthDate = player.BirthDate;
entity.DisplayAge = player.DisplayAge;
entity.Level = player.Level;
entity.PlayingHand = player.PlayingHand;
entity.BackhandType = player.BackhandType;
entity.PlayingStyle = player.PlayingStyle;
entity.Description = player.Description;
entity.UserId = player.UserId;
entity.Address = new Address();
entity.Address.AddressId = player.Address.AddressId;
entity.Address.Line1 = player.Address.Line1;
entity.Address.Line2 = player.Address.Line2;
entity.Address.City = player.Address.City;
entity.Address.State = player.Address.State;
entity.Address.ZipCode = player.Address.ZipCode;
_entities.AddToPlayer(entity);
_entities.SaveChanges();
return entity.PlayerId;
}
I am new to using EF. Any pointers is greatly appreciated.
Are you using EF 1.0?
Mapping entity properties to FK columns in EF 1.0 is not supported, you have to use a relationship and navigation properties.
I had a similar problem and set 'Identity Specification' to 'No'. After an update of my EDMX the problem went away.
精彩评论