【问题标题】:Identity Specification false Entity Code First身份规范假实体代码优先
【发布时间】:2014-01-15 17:11:54
【问题描述】:

我有 2 节课:

[Table("People")]
    public class Person : IPerson
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public string FirstName { get; set; }

        public Person()
        {
            Results = new Collection<Result>();
        }
        public string LastName { get; set; }

        public string Name
        {
            get
            {
                return FirstName + " " + LastName;
            }
            set{}
        }

        public string Email { get; set; }
        public DateTime? LastModified { get; set; }
        public virtual ICollection<Result> Results { get; set; }
    }

[Table("UserProfile")]

    public class UserProfile : Person
    {
        public UserProfile()
        {
            Faculty = new Faculty();
            Projects = new Collection<Project>();
        }
        public string UserName { get; set; }
        public string CNP { get; set; }
        public virtual Faculty Faculty { get; set; }
        public virtual ICollection<Project> Projects { get; set; }
    }

每次我使用 EF CodeFirst 生成我的数据库并尝试运行种子方法时,我都会收到错误消息。 更新条目时出错。有关详细信息,请参阅内部异常。 System.Data.SqlClient.SqlException:当 IDENTITY_INSERT 设置为 OFF 时,无法在表“UserProfile”中插入标识列的显式值。您可以在此处找到更多信息Seeding Membership with custom data Entity framework

我发现要在从包管理器控制台运行 update-database 后修复它,我必须在服务器资源管理器中打开表并将 Id 中的 Idendtity Specification 设置为 false >UsersProfile 表,我的问题是我可以对我的模型做些什么,这样我就不必每次重新生成我的 Db 时都这样做了。

我从这里Identity specification set to false 尝试了答案,但由于继承(我认为)我得到了

Conflicting configuration settings were specified for property 'Id' on type 'Prometheus.Models.Person': 
    DatabaseGeneratedOption = None conflicts with DatabaseGeneratedOption = Identity

谢谢。

【问题讨论】:

  • 种子方法会发生什么?什么是“错误”?
  • 您想要DatabaseGeneratedOption.Identity 还是DatabaseGeneratedOption.None?这里需要解决哪个问题?冲突还是种子?如果是冲突,OnModelCreating 中有任何 Fluent API 代码吗?如果是Seed,请向我们展示该代码和错误。
  • @GertArnold 你好,错误是更新条目时发生错误。有关详细信息,请参阅内部异常。 System.Data.SqlClient.SqlException:当 IDENTITY_INSERT 设置为 OFF 时,无法在表“UserProfile”中插入标识列的显式值。你可以在这里找到更多stackoverflow.com/questions/19934366/…
  • @Colin 老实说,我真的不知道,我认为“UserProfile”表和 People 的 DatabaseGeneratedOption.Identity 没有,但 UserProfile 继承自 People,这是我的问题。如果不将身份规范设置为 false,则使用我得到的错误更新问题

标签: c# asp.net asp.net-mvc entity-framework ef-code-first


【解决方案1】:

我不完全确定这里到底发生了什么,但我知道UserProfile 表通常是通过调用WebSecurity.InitializeDatabaseConnection 而不是迁移代码创建的,这将放入一个IDENTITY 字段。所以这里WebSecurity和Entity Framework有冲突。

然后您将继承添加到图片中,因为您已指定表名,所以它是 Table per Type - 实体框架希望它使用 shared primary key。所以它可能不希望UserProfile 表有 IDENTITY 字段。

我觉得你说的一针见血

UserProfile inherits from People, that's my problem

我会改变这种关系,让Person 有一个 UserProfile。我认为这可以更准确地模拟现实世界,如果您有任何不是用户的人,那么将关系设为可选会更容易。像这样:

[Table("People")]
public class Person : IPerson
{
    //avoids virtual call in constructor
    private ICollection<Result> _Results;

    public Person()
    {
        _Results = new Collection<Result>();
    }

    //no annotations required. 
    //An int field named Id is a database generated key by convention
    public int Id { get; set; }

    //Person has a UserProfile (optional)
    public int? UserProfileID { get; set; }

    public UserProfile UserProfile { get; set; }

    //etc

    public virtual ICollection<Result> Results
    {
        get { return _Results; }
        set { _Results = value; }
    }
}

public class UserProfile : ModelBase
{
    //UserProfile is always related to a Person
    public int PersonID { get; set; }

    public UserProfile Person { get; set; }

    //etc
}

你还会发现很多关于preferring composition over inheritance的东西。

否则,您需要深入研究迁移以获取以支持 TPT 的方式创建的表 - 但您还应该知道 switching identity on/off in migrations does not happen automatically。也许您可以在迁移中创建 UserProfile 表,而不是使用WebSecurity.InitializeDatabaseConnection

【讨论】:

  • 抱歉没有在我的问题中指定它,我在我的种子函数中调用 WebSecurity.InitializeDatabaseConnection。我不认为组合优于继承对我来说是一个选择,因为应用程序必须工作的方式,而且对于 EF,我需要默认构造函数。
  • 好的,这可能是一个错误的链接。请参阅我在编辑中添加的代码
猜你喜欢
  • 2017-08-25
  • 1970-01-01
  • 2012-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多