使用外键保存相关表的数据 - 实体框架

时间:2010-01-16 02:37:47

标签: asp.net-mvc entity-framework

我无法使用EntityFramework将我的实体保存到数据库。我有桌子,球员和地址。 Player表通过AddressId外键关系引用Address表。所以Player.AddressId指向Address.AddressId。

现在,当调用以下代码时,我收到一条错误消息:“ReferentialConstraint中的依赖属性映射到存储生成的列”

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

我是使用EF的新手。任何指针都非常感谢。

2 个答案:

答案 0 :(得分:0)

您使用的是EF 1.0吗?

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

答案 1 :(得分:0)

我遇到了类似的问题,并将“身份规范”设置为“否”。 更新了我的EDMX后,问题就消失了。

相关问题