c# - Entity Framework 5 DBGenerated Identity Using DBContext Template -
i'm quite new ef , have been reading many tutorials can, there's lot's of conflicting information out there. i've included code in case approach wrong, need figure out how ef allow db generate guid identity column, ef writing 0's.
i started ado.net entity data model, used in database first approach. manually set of primary keys storegeneratedpattern=identity within edmx. used ef5.x dbcontext generator, created classes.
the context looks like:
public partial class assets : dbcontext { public assets() : base(common.getconnstring(typeof(assets))) { } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { throw new unintentionalcodefirstexception(); } public dbset<building> buildings { get; set; } public dbset<device> devices { get; set; } public dbset<location> locations { get; set; } public dbset<manufacturer> manufacturers { get; set; } public dbset<model> models { get; set; } public dbset<operatingsystem> operatingsystems { get; set; } } the entities similar, here 1 looks like:
public partial class device : iguidid { public guid id { get; set; } public guid modelid { get; set; } public nullable<guid> operatingsystemid { get; set; } public guid locationid { get; set; } public string devicename { get; set; } public string description { get; set; } public byte[] binaryipaddress { get; set; } public string ipaddress { { if (this.binaryipaddress == null) return null; return string.join(".", this.binaryipaddress); } set { this.binaryipaddress = system.net.ipaddress.parse(value).getaddressbytes(); } } public virtual location location { get; set; } public virtual model model { get; set; } public virtual operatingsystem operatingsystem { get; set; } } if create new device , save it, instead of honoring settings defined in edmx writing guid defined in new device 00000... saving via unitofwork , repository pattern.
unitofwork:
public class unitofwork : unitofworkbase { private repository<device> _devicerepository; public repository<device> devicerepository { { if (_devicerepository == null) _devicerepository = new repository<device>(base.context); return _devicerepository; } } public unitofwork() : base(new model.assets()) { } } public class unitofworkbase : idisposable { private readonly dbcontext _context; protected dbcontext context { { return _context; } } public unitofworkbase(dbcontext context) { _context = context; } public void save() { _context.savechanges(); } // additional code dispose, etc. } repository:
public class repository<tentity> : genericrepository<tentity> tentity : class, iguidid { public repository(dbcontext context) : base(context) { } // additional code here(contains, etc.) } public class genericrepository<tentity> tentity : class { private readonly dbcontext _context; private readonly dbset<tentity> _dbset; public genericrepository(dbcontext context) { _context = context; _dbset = context.set<tentity>(); } public virtual ienumerable<tentity> all() { return _dbset.asenumerable(); } public virtual void delete(tentity entitytodelete) { if (_context.entry(entitytodelete).state == entitystate.detached) _dbset.attach(entitytodelete); _dbset.remove(entitytodelete); } public virtual void insert(tentity entity) { _dbset.add(entity); } public virtual void update(tentity entitytoupdate) { if (_context.entry(entitytoupdate).state == entitystate.detached) _dbset.attach(entitytoupdate); _context.entry(entitytoupdate).state = entitystate.modified; } } honestly behavior doesn't surprise me, if attach device 0000... id expect insert such, i'm not sure how resolve or things went wrong. should id's nullable though aren't in db? need mapping? sort of thing "worked" when still working linq sql.
thanks - derrick
i think need apply following attribute guid properties
[databasegenerated(databasegeneratedoption.identity)] public guid id { get; set; } you need add following namespace class
using system.componentmodel.dataannotations.schema;
Comments
Post a Comment