asp.net - Adding additional properties to an entity framework 4 code first CTP 5 entity -
i using asp.net mvc 3
, entity framework code first ctp 5
. wondering if possible add additional properties not mapped table column?
i haved news class , defined such:
public class news : entity { public int newsid { get; set; } public string title { get; set; } public string body { get; set; } public bool active { get; set; } }
my database context class:
public class mycontext : dbcontext { public dbset<news> newses { get; set; } }
in entity class have property defined like:
public ilist<ruleviolation> ruleviolations { get; set; }
i have not code part yet, want broken rules added list when object validated. error getting is:
one or more validation errors detected during model generation: system.data.edm.edmentitytype: : entitytype 'ruleviolation' has no key defined. define key entitytype. system.data.edm.edmentityset: entitytype: entityset ruleviolations based on type ruleviolation has no keys defined.
here reposity code:
public news findbyid(int newsid) { return context.database.sqlquery<news>("news_findbyid @newsid", new sqlparameter("newsid", newsid)).firstordefault(); }
update 2011-03-02:
here entity
class:
public class entity { public ilist<ruleviolation> ruleviolations { get; set; } public bool validate() { // still needs coded bool isvalid = true; return isvalid; } }
here ruleviolation
class:
public class ruleviolation { public ruleviolation(string parametername, string errormessage) { parametername = parametername; errormessage = errormessage; } public string parametername { get; set; } public string errormessage { get; set; } }
here context class:
public class mycontext : dbcontext { public dbset<news> newses { get; set; } protected override void onmodelcreating(modelbuilder modelbuilder) { modelbuilder.entity<news>().ignore(n => n.ruleviolations); } }
you can ignore type using fluent api adding ignore rule onmodelcreating
method of mycontext
class
public class mycontext : dbcontext { public dbset<news> newses { get; set; } protected override void onmodelcreating(modelbuilder builder) { builder.ignore<ruleviolation>() } }
or can ignore property using notmapped
attribute
public class enitity { [notmapped] public ilist<ruleviolation> ruleviolations { get; set; } //other properties here }
and entity framework ignore property.
Comments
Post a Comment