简体   繁体   中英

Updating EF model created using code first approach

I am working on a project that uses EF created with code first approach. 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). 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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