C# 身份说明首先是错误的实体代码
我有两门休闲课:C# 身份说明首先是错误的实体代码,c#,asp.net,asp.net-mvc,entity-framework,ef-code-first,C#,Asp.net,Asp.net Mvc,Entity Framework,Ef Code First,我有两门休闲课: [Table("People")] public class Person : IPerson { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public string FirstName { get; set; } public
[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; }
}
谢谢。我不完全确定这里到底发生了什么,但我知道
UserProfile
表通常是通过调用WebSecurity.InitializeDatabaseConnection
而不是迁移代码创建的,这将放在标识字段中。因此,WebSecurity
和实体框架之间存在冲突
然后将继承添加到图片中,因为您已经指定了表名,所以它就是—实体框架希望使用一个表名。因此,它可能不希望UserProfile
表具有标识字段
我想你说的一针见血
我会改变这种关系,让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
}
[表格(“人员”)]
公共类人士:IPerson
{
//避免构造函数中的虚拟调用
私人ICollection_结果;
公众人士()
{
_结果=新集合();
}
//不需要注释。
//名为Id的int字段是由约定生成的数据库密钥
公共int Id{get;set;}
//个人具有用户配置文件(可选)
public int?UserProfileID{get;set;}
公共用户配置文件用户配置文件{get;set;}
//等
公共虚拟ICollection结果
{
获取{返回_结果;}
设置{u Results=value;}
}
}
公共类UserProfile:ModelBase
{
//UserProfile始终与某人相关
公共int PersonID{get;set;}
公共用户配置文件Person{get;set;}
//等
}
你还会发现很多关于你的东西
否则,您需要深入迁移以获得以支持TPT的方式创建的表,但您也应该知道。也许您可以在迁移中创建
UserProfile
表,而不是使用WebSecurity。InitializeDatabaseConnection
种子方法会发生什么?什么是“错误”?您想要DatabaseGeneratedOption.Identity
还是DatabaseGeneratedOption.None
?这里需要解决哪个问题?冲突还是种子?如果是冲突,您在建模创建中是否有流畅的API代码?如果是种子
请向我们显示该代码和错误。@GertArnold hello该错误是更新条目时发生的错误。有关详细信息,请参见内部异常。System.Data.SqlClient.SqlException:当identity_insert设置为OFF时,无法在表“UserProfile”中为identity列插入显式值。你可以在这里找到更多@Colin我真的不知道,我认为对于“UserProfile”表和DatabaseGeneratedOption.Identity for People,但是UserProfile继承自People,这是我的问题。如果未将Idendtity规范设置为false,则使用错误更新了问题。很抱歉,在我的问题中未指定它。我正在种子函数中调用WebSecurity.InitializeDatabaseConnection。我不认为组合优于继承是我的选择,因为应用程序必须以这种方式工作,除此之外,对于EF,我需要默认构造函数。好吧,这可能是一个不好的链接。查看我在编辑中添加的代码
Conflicting configuration settings were specified for property 'Id' on type 'Prometheus.Models.Person':
DatabaseGeneratedOption = None conflicts with DatabaseGeneratedOption = Identity
UserProfile inherits from People, that's my problem
[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
}