entity framework - EF CF Mapping complex relationship with Fluent API -



entity framework - EF CF Mapping complex relationship with Fluent API -

i trying create next constraint in model tag object's tagtype valid. a valid tagtype 1 operatingcompanyid matches tag's website's operatingcompanyid. realize seems convoluted makes sense business standpoint:

an operating company has websites. websites contain tags. tags have tagtype(singular). tagtypes same across operating companies, meaning if 1 operating company has 20 tagtypes , 5 websites, 20 tagtypes should able used across fives of websites. want ensure tag's tagtype cannot 1 associated operatingcompany.

what best way create constraint in model? need alter poco, or utilize fluent api?

thanks in advance!

[table("operatingcompanies")] public class operatingcompany : configobject { public operatingcompany() { websites = new list<website>(); } [required(errormessage = "name required field operating company.")] [maxlength(100, errormessage = "name cannot exceed 100 characters.")] public string name { get; set; } public virtual icollection<website> websites { get; set; } } [table("websites")] public class website : configobject { public website() { webobjects = new list<webobject>(); } [required(errormessage = "url required field web site.")] [maxlength(100, errormessage = "url cannot exceed 100 characters web site.")] [regularexpression(@"\b(https?|ftp|file)://[-a-za-z0-9+&@#/%?=~_|!:,.;]*[-a-za-z0-9+&@#/%=~_|]", errormessage = "the value entered not valid url.")] public string url { get; set; } public operatingcompany operatingcompany { get; set; } [required(errormessage = "you must associate web site operating company.")] public guid operatingcompanyid { get; set; } [inverseproperty("website")] public virtual icollection<webobject> webobjects { get; set; } } [table("tags")] public class tag : configobject { [required(errormessage = "name required field tag.")] [maxlength(100, errormessage = "name cannot exceed 100 characters tag.")] public string name { get; set; } public tagtype tagtype { get; set; } [required(errormessage = "you must associate tag tag type.")] public guid tagtypeid { get; set; } public website website { get; set; } [required(errormessage = "you must associate tag web site.")] public guid websiteid { get; set; } } [table("tagtypes")] public class tagtype : configobject { [required(errormessage = "name required field tag.")] [maxlength(100, errormessage = "name cannot exceed 100 characters tag type.")] public string name { get; set; } public operatingcompany operatingcompany { get; set; } [required(errormessage = "you must associate tag type operating company.")] public guid operatingcompanyid { get; set; } }

one way enforce constraint take advantage of new validation feature introduced part of new dbcontext api in ef 4.1. can write custom validation rule create sure tag types given company's website selected valid tag types company. next shows how can done:

public abstract class configobject { public guid id { get; set; } } public class operatingcompany : configobject, ivalidatableobject { public string name { get; set; } public virtual icollection<website> websites { get; set; } public virtual list<tagtype> tagtypes { get; set; } public ienumerable<validationresult> validate(validationcontext validationcontext) { var alltagtypes = (from w in websites t in w.tags select t.tagtype); if (!alltagtypes.all(wtt => tagtypes.exists(tt => tt.id == wtt.id))) { yield homecoming new validationresult("one or more of website's tag types don't belong company"); } } } public class website : configobject { public string url { get; set; } public guid operatingcompanyid { get; set; } public virtual icollection<tag> tags { get; set; } public operatingcompany operatingcompany { get; set; } } public class tag : configobject { public string name { get; set; } public guid tagtypeid { get; set; } public guid websiteid { get; set; } public tagtype tagtype { get; set; } public website website { get; set; } } public class tagtype : configobject { public string name { get; set; } public guid operatingcompanyid { get; set; } public operatingcompany operatingcompany { get; set; } } public class context : dbcontext { public dbset<operatingcompany> operatingcompanies { get; set; } public dbset<website> websites { get; set; } public dbset<tag> tags { get; set; } public dbset<tagtype> tagtypes { get; set; } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.entity<tag>().hasrequired(t => t.website) .withmany(w => w.tags) .hasforeignkey(t => t.websiteid) .willcascadeondelete(false); } }

as result, ef invoke validate method each time phone call dbcontext.savechanges() save operatingcompany object database , ef throw (and abort transaction) if method yields validation error. can proactively check validation errors calling getvalidationerrors method on dbcontext class retrieve list of validation errors within model objects working with.

it worth noting since utilize domain model view model mvc layer, mvc recognize , honor validation rule , can check validation result looking modelstate in controller. checked in 2 places, 1 time in presentation layer mvc , 1 time in end ef.

hope helps.

entity-framework ef-code-first fluent-interface object-relationships

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -