2010-01-16 102 views
0

我无法使用EntityFramework将实体保存到数据库。我必须要桌子,球员和地址。播放器表通过AddressId外键关系具有对地址表的引用。所以Player.AddressId指向Address.AddressId。使用外键保存相关表的数据 - 实体框架

现在调用下面的代码时,我得到一个错误,指出:“在ReferentialConstraint从属属性映射到存储生成的列”我是新来使用EF

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; 
} 

。任何指针非常感谢。

+0

有两个问题:Player.Address.AddressId的值是否大于零? 并且Address.AddressId是一个基于整数的IDENTITY列? – willbt 2010-01-16 03:32:54

+0

Address.AddressId是一个整数标识列autoincremented。在所提供的代码实现中,由于尚未创建地址,因此Player.Address.AddressId为null。 – 2010-01-16 03:38:00

+0

我想我不确定是否通过创建播放器实体地址实体也将被创建,因为两个表都是相关的。我想知道使用EF保存相关表格的最佳方法。 – 2010-01-16 03:39:31

回答

0

您使用的是EF 1.0吗?

不支持将实体属性映射到EF 1.0中的FK列,您必须使用关系和导航属性。

0

我有一个类似的问题,并设置'身份规范'为'否'。 更新我的EDMX后,问题就消失了。