【发布时间】: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