Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing required properties from EF Model, because it is read only

I have a project with a small data model that consumes EF models in a read-only fashion.

I don't want the full set of columns in the models, but I am required to have them if they are non-nullable and have no default values.

How can I avoid including such columns? Can I put EF into some read-only mode in the data model which will allow me to remove the columns from the entities?

The reason I want to do this is because by reducing the columns in my data models only to what I need I reduce the columns the model needs to return in queries and I reduce the risk of breaking my data consumers if the schema changes.

EDIT: My schema has tables with NOT NULL columns having no default values. As far as I can tell I am required to have these columns included in my edmx. In my situation I only have read-only context so I don't want these columns to be included in my edmx at all.

If I can prevent the columns from being in the data model I can prevent many issues arising from changing the schema. The only solution I've found so far is to build the datamodel by pointing to a "fake" database which doesn't have the columns!

like image 652
Matthew Avatar asked Jul 02 '15 23:07

Matthew


3 Answers

According to MSDN, a QueryView is designed for exactly the scenario you describe.

The QueryView element in mapping specification language (MSL) defines a read-only mapping between an entity type or association in the conceptual model and a table in the underlying database.

You can define query views to enable the following scenarios:

Define an entity in the conceptual model that doesn't include all the properties of the entity in the storage model. This includes properties that do not have default values and do not support null values.

...(more scenarios on the page)

You can't use the designer for this, but it looks simple enough to do by hand.

Here is a link to the relevant MSDN documentation:
https://msdn.microsoft.com/en-us/library/cc716798(v=vs.100).aspx

In case that link goes dead, do a search for QueryView MSL.

like image 113
Taekahn Avatar answered Oct 06 '22 00:10

Taekahn


Are you looking for data annotation [NotMapped] ?

If you use it in a property inside model, it will not pass to the db.

like image 34
Leandro Soares Avatar answered Oct 05 '22 23:10

Leandro Soares


If you're willing to use code-first in stead of database-first this could be really simple.

Take for instance Microsoft's sample database School. It has a table Course with a number of required fields. But it's possible to map a class with only a small subset of these fields...

class Course
{
    public int CourseID { get; private set; }
    public string Title { get; private set; }
}

...to this table, ignoring required fields Credits and DepartmentID.

The fluent mapping in the context's OnModelCreating override is

modelBuilder.Entity<Course>().HasKey(a => a.CourseID);
modelBuilder.Entity<Course>().Property(a => a.CourseID)
                   .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

As you see, I defined the properties with a private setter. EF has no problems with that and it communicates to any consumer of the model that the model is read-only.

On top of this you could even map non-key properties as Computed:

modelBuilder.Entity<Course>().Property(a => a.Title)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);

Now it's impossible to update any property value inadvertently, because EF simply won't include them in UPDATE statements.

like image 38
Gert Arnold Avatar answered Oct 06 '22 01:10

Gert Arnold