0

I'm working on generating code (models) from my database. I'm not sure how to, if possible, setup my database to create a relationship including cardinality. Is this something that can be done, or am I stuck with simply generating models that match a table without relationships or cardinality because I did a data-first design?

Oh, and in case I'm using the incorrect term, cardinality is the type of relationship (one-to-one, one-to-many, many-to-many). That way my models will generate with a reference to another model, or to an ICollection<T> of models.

For example:

public class OrderInformation
{
     public virtual int OrderId { get; set; }

     public virtual DateTime OrderDate { get; set; }

     public virtual BillingInformation BillingInfo { get; set; }

     public virtual Address DeliveryAddress { get; set; }

     public virtual ICollection<ItemInformation> ShoppingCart { get; set; }
}
myermian
  • 31,823
  • 24
  • 123
  • 215
  • Are you using Entity Framework? – Tim B Jun 13 '13 at 01:48
  • Most of my database generation was inside T4 templates. You can leverage Sql Management Objects since relations are preserved. I also wrote a [MultiMapper for Dapper](https://gist.github.com/Romoku/5310100) which can handle Many-to-Many relationships. – Dustin Kingen Jun 13 '13 at 01:51
  • @TimB: No, I'm not using Entity Framework... but even if I were, wouldn't the database setup have to be the same in order for EF to generate models correctly? – myermian Jun 13 '13 at 02:02
  • I asked because other tooling might be able to pull more information from the database than EF does. As far as I know, there is nothing you can do to change how EF generates code. – Tim B Jun 13 '13 at 02:05

0 Answers0