简体   繁体   English

更新使用代码优先方法创建的EF模型

[英]Updating EF model created using code first approach

I am working on a project that uses EF created with code first approach. 我正在使用使用通过代码优先方法创建的EF的项目。 The model was created by someone else. 该模型是由其他人创建的。 In the database table there was no primary key and all the fields were mandatory. 在数据库表中没有主键,并且所有字段都是必填字段。 Now we need a column to be auto incremented primary key and another column to be optional. 现在我们需要一列自动递增主键,另一列是可选的。 We have made these changes in the database table but we are getting 'Entity Validation Error' since we are not passing the "ID"(probably the reason). 我们已经在数据库表中进行了这些更改,但是由于未传递“ ID”(可能是原因),因此我们收到了“实体验证错误”。 What are the changes and where should they be written? 有哪些更改,应将这些更改写在哪里? Here is the class that was created: 这是创建的类:

public int ID { get; set; }//This needs to be auto incremented primary key.
public int UserID{ get; set; }
public string Name { get; set; }
public string Address { get; set; }
public bool IsActive { get; set; }
public bool Deleted { get; set; }
public int ModifiedBy { get; set; }
public System.DateTime DateCreated { get; set; }
public System.DateTime DateModified { get; set; }//This field needs to be optional.

You need to set the Key and DatabaseGenerated attributes for the new Id Property. 您需要为新的Id属性设置Key和DatabaseGenerated属性。

[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM