locked
E-F code first database design: Choosing which model classes are part of the database, and which ones are not RRS feed

  • Question

  • User-734459410 posted

    When creating model classes in a project that is using code-first with E-F, does E-F only create database entities for classes that you have defined a DbSet for?  For example, if I had the following segment (assuming Picture, Album, and Picture_Album are all data model classes that I've defined as Public classes:

    public DbSet<Picture> Pictures { get; set; }
    public DbSet<Album> Albums { get; set; }
    public List<Picture_Album> Pictures_Albums { get; set; }

    E-F would only create tables for the Picture and Album classes, correct?  Since I defined DbSets for those classes, and it would ignore the Picture_album list (which that table would be used internally in memory and not need to be stored in the database as a table, since it's not defined with a DbSet.

    I basically need to be able to create "view models" (like you find in MVC) for storing data in memory that does not need to be persisted/written to the database.  I'm also doing this mostly in code, and not using the EDMX designer/wizard (I don't think that matters though).

    Friday, January 12, 2018 6:01 AM

All replies

  • User1120430333 posted

    When creating model classes in a project that is using code-first with E-F, does E-F only create database entities for classes that you have defined a DbSet for? 

    It's called the virtual model, which are classes/objects that represent the tables of the database and are used for CRUD operations with the database. Another name for the classes/objects in the virtual model is called the persistence model.

    Custom objects that you need that are not part of the persistence model should not be placed in the persistence model used by the ORM, IMO. You can create a VS project folder, a namespace -- namespace separation, and place the custom class/object there.

    Friday, January 12, 2018 9:28 AM
  • User753101303 posted

    Hi,

     If I remember it will pick whatever is linked to types used in a Db (ie if Album have another class as a property it should be picked as well even if not explicitely exposed as a DbSet).

    If a class is not persisted I'm not sure it really make sense to expose it on your DbContext.

    At worst you can explicitely exclude a class for example using https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.schema.notmappedattribute(v=vs.110).aspx

    Friday, January 12, 2018 9:42 AM
  • User-832373396 posted

    Hi cbassett,

    E-F would only create tables for the Picture and Album classes, correct?

    Yes. 

    DbSet type properties for the entities which map to database tables and views. and it is required;

    Relation document:

    http://www.entityframeworktutorial.net/entityframework6/dbset.aspx 

    I basically need to be able to create "view models" (like you find in MVC) for storing data in memory that does not need to be persisted/written to the database

    I guess that you get the newest memory and assign it to Pictures_Albms, and this is a way.

    With regards, Angelina Jolie

    Wednesday, January 17, 2018 7:38 AM