poco - Entity Framework 4 - One-To-Many Relationship with a view with CTP5 (code first) -


i'm attempting map 1-m relationship between 2 entities first 1 mapped table , second 1 taken view.

the involved entities are:

public class institute {     public int id { get; set; }     public string name { get; set; }     //...     public virtual icollection<text> texts { get; set; } }  public class text {     public int instituteid { get; set; }     public int textid { get; set; }     public string name { get; set; }     public string value { get; set; }     public bool isrequired { get; set; }     public int? maxlength { get; set; } } 

the relevant mapping code is:

private void mapinstitutetext(entitytypeconfiguration<institutetext> text) {     //from view     text.haskey(i => i.instituteid)         .totable("vwinstitutetexts");      text.property(i => i.instituteid)         .hascolumnname("fkinstituteid")         .isrequired();      text.property(i => i.textpropertyid)         .hascolumnname("fktextpropertyid")         .isrequired();      text.property(i => i.name)         .hascolumnname("name")         .isrequired();      text.property(i => i.value)         .hascolumnname("value");      text.property(i => i.isrequired)         .isrequired();      text.property(i => i.maxlength); }  private void mapinstitute(entitytypeconfiguration<institute> institute) {     institute.haskey(i => i.id)         .totable("institutes");      institute.property(i => i.id)         .hascolumnname("id")         .isrequired();      institute.property(i => i.name)         .hascolumnname("name")         .hasmaxlength(128)         .isrequired();      institute         .hasmany(i => i.texts)         .withrequired()         .hasforeignkey(t => t.instituteid); } 

the problem model not validated:

initialization method studentum.core.tests.institutetests.initialize threw exception. system.typeinitializationexception: system.typeinitializationexception: type initializer 'studentum.core.fluentcorerepositoryfactory' threw exception. ---> system.data.entity.modelconfiguration.modelvalidationexception: 1 or more validation errors detected during model generation:

system.data.edm.edmassociationend: : multiplicity not valid in role 'institute_innerinstitutetexts_target' in relationship 'institute_innerinstitutetexts'. because dependent role refers key properties, upper bound of multiplicity of dependent role must 1. (the names of exception can not match because recreated of code post)

if remove ".hasforeignkey(t => t.instituteid);" generated query contains reference field called instituteid1 not present in queried view

exec sp_executesql n'select  [extent1].[fkinstituteid] [fkinstituteid],  [extent1].[fktextpropertyid] [fktextpropertyid],  [extent1].[name] [name],  [extent1].[value] [value],  [extent1].[isrequired] [isrequired],  [extent1].[maxlength] [maxlength],  [extent1].[instituteid1] [instituteid1] [institute].[vwinstitutetexts] [extent1] [extent1].[instituteid1] = @entitykeyvalue1',n'@entitykeyvalue1 int',@entitykeyvalue1=1360 

any suggestion? thanks.

apparently view seems not having required keys. try changing

text.haskey(i => i.instituteid)         .totable("vwinstitutetexts"); 

to

 text.haskey(i => new {i.instituteid, i.textid}).totable("vwinstitutetexts") 

this recognize relationship key textid.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -