user5214718
user5214718

Reputation:

Entity Framework: How to map the columns to the database?

On of my model's properties needs to be from a custom type. This custom type has two properties [ID, TextToShow]. What I want is to save only the value of ID of the custom type in the database. The other value which is TextToShow is used only when retrieving from database.

I am using EF6,VS2015 and C# connected to SQL Server

Upvotes: 0

Views: 82

Answers (3)

Tomas
Tomas

Reputation: 106

If I get it right, what you need is a Complex Type It should be easy to set up with EF Designer. Or you can check this blog post by Brady Holt if you're using code first approach.

Once you add complex type mark the field you want to ignore with [NotMapped] attribute

Upvotes: 0

user5214718
user5214718

Reputation:

Solution Found: 1) I changed the name of ID field to something else so that EF to recognize it as a key.

2) Marked TextToShow as [NotMapped]

The result: Only the value of ID field [which have been renamed] is being stored in the database.

*

Will keep this conversation open for anyone who face the same problem

    *

Upvotes: 0

will
will

Reputation: 1511

If you want to Entity Framework to ignore a column in the database, use the NotMapped attribute on your model's property.

class Example {
    public Guid Id { get; set; }
    [NotMapped]
    public string TextToShow { get; set; }
}

Upvotes: 1

Related Questions