Sunday, 15 January 2012

c# - How to ignore nested enums from mapping -



c# - How to ignore nested enums from mapping -

i have several reference entities nested enums convenience. example:

public class statusa { public enum values { active = 1, inactive = 2, inprogress = 3 } } public class statusb { public enum values { sent = 1, accepted = 2, expired = 3 } } public class entitya { public statusa.values status {get; set;} } public class entityb { public statusb.values status {get; set;} }

i'm getting next exception @ time of model configuration: the type 'statusa+values' , type 'statusb+values' both have same simple name of 'values' , cannot used in same model. types in given model must have unique simple names. utilize 'notmappedattribute' or phone call ignore in code first fluent api explicitly exclude property or type model.

trying prepare found out notmappedattribute not applicable enums. tried fluent api .ignore<t> (which requires ref type, not enum) , .ignore(ienumerable<type>), no luck. google search not helpful.

is there other way exclude enums model?

you should enumeration type in model if include property of type. if that, there cannot way of ignoring type, without ignoring property. , testing shows ignoring property sufficient ignore type.

here's minimal finish test programme exception you're getting:

using system.data.entity; using system.data.entity.infrastructure; public class { public int id { get; set; } public e p { get; set; } // #1 public enum e { } } public class b { public int id { get; set; } public e p { get; set; } // #2 public enum e { } } static class programme { static void main() { var modelbuilder = new dbmodelbuilder(dbmodelbuilderversion.latest); modelbuilder.entity<a>(); modelbuilder.entity<b>(); var model = modelbuilder.build(new dbproviderinfo("system.data.sqlclient", "2012")); } }

it should obvious long #1 , #2 both part of model, cannot avoid mapping enumeration types. , if remove either #1 or #2 (or both), or mark them notmapped attribute, see no longer exception you're getting now.

you may able avoid renaming enumeration type if can avoid mapping property of type, so:

public class { public int id { get; set; } public int pasint { get; set; } [notmapped] public e p { { homecoming (e) pasint; } set { pasint = (int) value; } } public enum e { } }

this old approach required when ef didn't back upwards enumeration types @ all. unfortunately, approach mean query context.as.where(a => a.p == e.c) won't work, because model doesn't know p property. need written context.as.where(a => a.pasint == (int)e.c). depending on needs, may enough, though.

c# entity-framework ef-code-first entity-framework-6 ef-fluent-api

No comments:

Post a Comment