asp.net mvc - C# MVC Code First Complex Model -
asp.net mvc - C# MVC Code First Complex Model -
i have 1 table "adverts" stores basic info adverts (eg: name, excerpt, creation date...), , need store more detailed info in separate table, but, here's problem. adverts can different type (sell, buy, rent, ...), category (residential, commercial, ...), so, detailed info different (eg: commercial advert don't need kitchen area property). want create few models describe detailed info specific type or category
here's adverts model:
[table("adverts_adverts")] public class advert { [key] [databasegenerated(databasegeneratedoption.identity)] public guid advertid { get; set; } public virtual metadata metadata { get; set; } [required] [datatype(datatype.text)] public string name { get; set; } [datatype(datatype.html), allowhtml] public string content { get; set; } [foreignkey("section")] public guid sectionid { get; set; } public virtual section section { get; set; } [foreignkey("category")] public guid categoryid { get; set; } public virtual category category { get; set; } [foreignkey("type")] public guid typeid { get; set; } public virtual type type { get; set; } public decimal cost { get; set; } [datatype("enum")] public currency currency { get; set; } [foreignkey("details")] public guid detailsid { get; set; } public virtual ?????????? details { get; set; } [foreignkey("user")] public string userid { get; set; } public virtual user user { get; set; } [readonly(true)] [datatype(datatype.datetime)] public datetime added { get; set; } [readonly(true)] [datatype(datatype.datetime)] public datetime updated { get; set; } public int32 views { get; set; } [readonly(true)] public status status { get; set; } ... }
here's detailed info model residential adverts:
[table("adverts_details")] public class residentialdetails { [key] [databasegenerated(databasegeneratedoption.identity)] public guid detailsid { get; set; } [foreignkey("advert")] public guid advertid { get; set; } public virtual advert advert { get; set; } [required] public int32 storeys { get; set; } [required] public int32 floor { get; set; } [required] public int32 rooms { get; set; } [required] public decimal totalarea { get; set; } [required] public decimal livingarea { get; set; } [required] public decimal kitchenarea { get; set; } ... }
and may commercial adverts:
[table("adverts_details")] public class commercialdetails { [key] [databasegenerated(databasegeneratedoption.identity)] public guid detailsid { get; set; } [foreignkey("advert")] public guid advertid { get; set; } public virtual advert advert { get; set; } [required] public int32 officescount { get; set; } [required] public int32 floor { get; set; } [required] public decimal totalarea { get; set; } ... }
so, how can access both, residentialdetails , commercialdetails, info within advert's property "details"?
(thank in advance)
this architecture problem, hard reply without finish understanding of business rules. can give general advice help along.
as much possible, remove complexity. i'm not sure "kitchen area property" is, can generalize @ all? based upon context, can phone call different, utilize differently, etc. if it's text field, can repurpose in other contexts. maybe residential advert it's "kitchen area" while maybe commercial it's "break room area". (i have no thought property for, i'm trying create point same property can have similar different meaning in different contexts).
if can't generalize, you'll need start working on inheritance strategies. create object graph. how these types , categories of adverts related. how different. ones supergroups of others, etc.? again, don't know business rules @ play, maybe need classes advert
, residentialadvert : advert
, commercialadvert : advert
. then, can add together additional properties these subclasses necessary.
you'll need decide on relational strategy. default, ef implement simple inheritance sti (single-table inheritance, aka table per hierarchy or tph short). in other words, classes above, end adverts
table discriminator
column. value column 1 of "advert", "residentaladvert", or "commercialadvert", indicating class should instantiated, of columns of subclasses reside in same table. benefit no joins necessary, detriment additional columns on subclasses must nullable or have default values. other possible strategies include, table per type (tpt), compositional strategry, or table per concrete type (tpc), every subtype gets own unique table fields supertypes.
c# asp.net-mvc model-view-controller modeling
Comments
Post a Comment