c# - Entity Framework 6 GUID as primary key: Cannot insert the value NULL into column 'Id', table 'FileStore'; column does not allow nulls -


i have entity primary key "id" guid:

public class filestore {     public guid id { get; set; }     public string name { get; set; }     public string path { get; set; } } 

and configuration:

protected override void onmodelcreating(dbmodelbuilder modelbuilder) {     modelbuilder.entity<filestore>().property(x => x.id).hasdatabasegeneratedoption(databasegeneratedoption.identity);     base.onmodelcreating(modelbuilder); } 

when try insert record following error:

cannot insert value null column 'id', table 'filestore'; column not allow nulls. insert fails.\r\nthe statement has been terminated.

i don't want generate guid manually. want insert record , id generated sql server. if set .hasdatabasegeneratedoption(databasegeneratedoption.identity), id column not identity column in sql server.

how can configure entity framework autogenerate guid in sql server?

in addition adding these attributes id column

[key] [databasegenerated(databasegeneratedoption.identity)] public guid id { get; set; } 

in migration should change createtable add defaultvaluesql property column i.e.:

id = c.guid(nullable: false, identity: true, defaultvaluesql: "newsequentialid()"), 

this prevent having manually touch database which, pointed out in comments, want avoid code first.


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -