entity framework - EF 4.4 Preventing AutoGen Navigation Properties and/or Relationships -



entity framework - EF 4.4 Preventing AutoGen Navigation Properties and/or Relationships -

i started db schema using ef , ran multiple issues when tring mannually modify clr's and/or db tables. first "employee_id" column ef placed in table. deleted it, dbo.edmmetadata , dbo.__migrationhistory tables , fumbled through run-time errors insued. now, i'm grapling next error:

a dependent property in referentialconstraint mapped store-generated column. column: 'employeeid'.

my implementation uses timecardentity clr has 3 computed columns. these columns happens map table's primary key. other table employeerecord.

goal) don't want ef auto map thse 3 columns. intend fill them myself due complications ef offers, can't tell ef stop creating navigation relationships and/or referential constraints.

point #1) have employeerecord table has guid id primary key, maps clr class employeerecord

point #2) have timecardentity table has has 3 computed columns called employeeid, managerid, divisionmanagerid relate employeerecord. null declared employeeid required, obviously, because can't have time card without declaring employee. managerid , divisionmanagerid filled later.

point #3) please don't inquire me "why these computed?", because there reason. alos sense illrelevant issue. in short, computed employeeid's (whether employee, manager or partition mananger), stored in xml property info of approval , signature of employee - provides non reputiation.

point #4) have 3 stored functions called fxgetemployeeid(xml), fxgetmanagerid(xml), , getdivisonmanagerid(xml). each of these used in computed columns employeeid, managerid , divisionmanagerid respectively.

here class declarations simplified brevity:

public enum timecardemployeetypeenum { employee, manager, divisionmanager } [datacontract] [serializable] [table("employeerecord", schema = "timecard")] public class employeerecord { #region exposed propert(y|ies) [datamember] public guid id { get; set; } /// <summary> /// customers internal company employee id. can null, ssn, lastly 4, or ever... /// included in case part of pains... /// </summary> [customvalidation(typeof(modelvalidator), "employeerecord_employeeid", errormessage = "employee id not valid.")] public string employeeid { get; set; } #endregion } [datacontract] [serializable] [table("timecardentry", schema = "timecard")] public class timecardentry { #region fellow member field(s) [nonserialized] xdocument m_timeentries; #endregion #region con/destructor(s) public timecardentry() { this.m_timeentries = "<root />".toxdocument(); } public timecardentry(guid employeeid) { if (employeeid == guid.empty) throw new argumentnullexception("employeeid"); this.m_timeentries = "<root />".toxdocument(); this.employeeid = employeeid; } #endregion #region exposed propert(y|ies) [notmapped] [ignoredatamember] public xdocument timeentries { { if (this.m_timeentries == null) { if (!string.isnullorempty(this.timeentriesxml)) this.m_timeentries = this.timeentriesxml.toxdocument(); } homecoming this.m_timeentries; } set { this.m_timeentries = value; if (this.m_timeentries != null) this.timeentriesxml = this.m_timeentries.tostring(); else this.timeentriesxml = null; this.onpropertychanged("timeentriesxml"); this.onpropertychanged("timeentries"); } } [datamember] [editorbrowsable(editorbrowsablestate.never)] [required] public string timeentriesxml { { if (this.m_timeentries == null) homecoming null; homecoming this.m_timeentries.tostring(); } set { this.m_timeentries = value.toxdocument(); this.onpropertychanged("timeentriesxml"); this.onpropertychanged("timeentries"); } } [ignoredatamember] [databasegenerated(system.componentmodel.dataannotations.schema.databasegeneratedoption.computed)] public guid? employeeid { { var attribute = this.m_timeentries.root.attribute("employeeid"); if (attribute != null) homecoming (guid)attribute; homecoming null; } set { if (this.validatesignature(timecardemployeetypeenum.manager)) throw new argumentexception("property cannot changed 1 time manager signature has been set.", "employeeid"); if (value != null && value.value != guid.empty) this.m_timeentries.root.setattributevalue("employeeid", value); else { var attribute = this.m_timeentries.root.attribute("employeeid"); if (attribute != null) attribute.remove(); } this.onpropertychanged("employeeid"); } } public virtual employeerecord employee { get; set; } [notmapped] [ignoredatamember] public datetime? employeeapprovaldate { { var attribute = this.m_timeentries.root.attribute("employeeapprovaldate"); if (attribute != null) homecoming (datetime)attribute; homecoming null; } set { if (this.validatesignature(timecardemployeetypeenum.manager)) throw new argumentexception("property cannot changed 1 time manager signature has been set.", "employeeapprovaldate"); if (value.hasvalue) this.m_timeentries.root.setattributevalue("employeeapprovaldate", value); else { var attribute = this.m_timeentries.root.attribute("employeeapprovaldate"); if (attribute != null) attribute.remove(); } this.onpropertychanged("employeeapprovaldate"); } } [notmapped] [ignoredatamember] public byte[] employeesignature { { var attribute = this.m_timeentries.root.attribute("employeesignature"); if (attribute != null) homecoming convert.frombase64string((string)attribute); homecoming null; } set { if (this.validatesignature(timecardemployeetypeenum.manager)) throw new argumentexception("property cannot changed 1 time manager signature has been set.", "employeesignature"); if (value != null) { if (value.length > 1024) throw new argumentexception("signature cannot larger 1kb.", "employeesignature"); this.m_timeentries.root.setattributevalue("employeesignature", convert.tobase64string(value)); } else { var attribute = this.m_timeentries.root.attribute("employeeapprovaldate"); if (attribute != null) attribute.remove(); } this.onpropertychanged("employeesignature"); } } [ignoredatamember] [databasegenerated(system.componentmodel.dataannotations.schema.databasegeneratedoption.computed)] public guid? managerid { { var attribute = this.m_timeentries.root.attribute("managerid"); if (attribute != null) homecoming (guid)attribute; homecoming null; } set { if (this.validatesignature(timecardemployeetypeenum.divisionmanager)) throw new argumentexception("property cannot changed 1 time partition manager signature has been set.", "managerid"); if (value.hasvalue) { if (value.value == guid.empty) throw new argumentnullexception("managerid"); this.m_timeentries.root.setattributevalue("managerid", value); } else { var attribute = this.m_timeentries.root.attribute("managerid"); if (attribute != null) attribute.remove(); } this.onpropertychanged("managerid"); } } public virtual employeerecord manager { get; set; } [notmapped] [ignoredatamember] public datetime? managerapprovaldate { { var attribute = this.m_timeentries.root.attribute("managerapprovaldate"); if (attribute != null) homecoming (datetime)attribute; homecoming null; } set { if (this.validatesignature(timecardemployeetypeenum.divisionmanager)) throw new argumentexception("property cannot changed 1 time partition manager signature has been set.", "managerapprovaldate"); if (value.hasvalue) this.m_timeentries.root.setattributevalue("managerapprovaldate", value); else { var attribute = this.m_timeentries.root.attribute("managerapprovaldate"); if (attribute != null) attribute.remove(); } this.onpropertychanged("managerapprovaldate"); } } [notmapped] [ignoredatamember] public byte[] managersignature { { var attribute = this.m_timeentries.root.attribute("managersignature"); if (attribute != null) homecoming convert.frombase64string((string)attribute); homecoming null; } set { if (this.validatesignature(timecardemployeetypeenum.divisionmanager)) throw new argumentexception("property cannot changed 1 time partition manager signature has been set.", "managersignature"); if (value != null) { if (value.length > 1024) throw new argumentexception("signature cannot larger 1kb.", "managersignature"); this.m_timeentries.root.setattributevalue("managersignature", convert.tobase64string(value)); } else { var attribute = this.m_timeentries.root.attribute("managersignature"); if (attribute != null) attribute.remove(); } this.onpropertychanged("managersignature"); } } [ignoredatamember] [databasegenerated(system.componentmodel.dataannotations.schema.databasegeneratedoption.computed)] public guid? divisionmanagerid { { var attribute = this.m_timeentries.root.attribute("divisionmanagerid"); if (attribute != null) homecoming (guid)attribute; homecoming null; } set { if (value.hasvalue) { if (value.value == guid.empty) throw new argumentnullexception("divisionmanagerid"); this.m_timeentries.root.setattributevalue("divisionmanagerid", value); } else { var attribute = this.m_timeentries.root.attribute("divisionmanagerid"); if (attribute != null) attribute.remove(); } this.onpropertychanged("divisionmanagerid"); } } public virtual employeerecord divisionmanager { get; set; } [notmapped] [ignoredatamember] public datetime? divisionmanagerapprovaldate { { var attribute = this.m_timeentries.root.attribute("divisionmanagerapprovaldate"); if (attribute != null) homecoming (datetime)attribute; homecoming null; } set { if (value.hasvalue) this.m_timeentries.root.setattributevalue("divisionmanagerapprovaldate", value); else { var attribute = this.m_timeentries.root.attribute("divisionmanagerapprovaldate"); if (attribute != null) attribute.remove(); } this.onpropertychanged("divisionmanagerapprovaldate"); } } [notmapped] [ignoredatamember] public byte[] divisionmanagersignature { { var attribute = this.m_timeentries.root.attribute("divisionmanagersignature"); if (attribute != null) homecoming convert.frombase64string((string)attribute); homecoming null; } set { if (value != null) { if (value.length > 1024) throw new argumentexception("signature cannot larger 1kb.", "divisionmanagersignature"); this.m_timeentries.root.setattributevalue("divisionmanagersignature", convert.tobase64string(value)); } else { var attribute = this.m_timeentries.root.attribute("divisionmanagersignature"); if (attribute != null) attribute.remove(); } this.onpropertychanged("divisionmanagersignature"); } } #endregion }

this db context declaration

public sealed class databasecontext : dbcontext { public databasecontext(bool autodetectchangesenabled = false, bool lazyloadingenabled = false, bool proxycreationenabled = false, bool validateonsaveenabled = false) { this.configuration.autodetectchangesenabled = autodetectchangesenabled; this.configuration.lazyloadingenabled = lazyloadingenabled; this.configuration.proxycreationenabled = proxycreationenabled; this.configuration.validateonsaveenabled = validateonsaveenabled; } public dbset<employeerecord> employeerecords { get; set; } public dbset<timecardentry> timecards { get; set; } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.conventions.remove<system.data.entity.infrastructure.includemetadataconvention>(); } }

update have add together observed behavior of ef. when add together "notmappedattribute" employeeid column of timecardentry, issue. ef addes "employee_id" column auto-gen schema. see tsql profile trace below:

exec sp_executesql n'select [limit1].[c1] [c1], [limit1].[id] [id], [limit1].[timeentriesxml] [timeentriesxml], [limit1].[managerid] [managerid], [limit1].[divisionmanagerid] [divisionmanagerid], [limit1].[createdby] [createdby], [limit1].[created] [created], [limit1].[updatedby] [updatedby], [limit1].[updated] [updated], [limit1].[employee_id] [employee_id] ( select top (2) [extent1].[id] [id], [extent1].[timeentriesxml] [timeentriesxml], [extent1].[managerid] [managerid], [extent1].[divisionmanagerid] [divisionmanagerid], [extent1].[createdby] [createdby], [extent1].[created] [created], [extent1].[updatedby] [updatedby], [extent1].[updated] [updated], [extent1].[employee_id] [employee_id], 1 [c1] [timecard].[timecardentry] [extent1] [extent1].[id] = @p0 ) [limit1]',n'@p0 uniqueidentifier',@p0='10f3e723-4e12-48cd-8750-5922a1e42aa3'

ef trying declare employee_id in database because needs column foreign key employee table. cannot utilize employeeid property , columns foreign key because declared computed - foreign keys in ef must not declared computed or identity (it not supported).

solution model either requires abandoning navigation properties , work ids (and loading related employees manually) or abandoning computed columns - can imagine both options may quite annoying.

entity-framework entity-framework-4

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -