0

EF beginner here.

How am I supposed to make changes in database model using Entity Framework?

I mean in DB model like changing datatypes of columns, adding attributes etc.?

E.g. I have string Password property in User table and I want to add [DataType(DataType.Password)] Attribute or [Required] or anything.

How am I supposed to do that? Of course along with applying changes to my DB? I created DB model from mdf local file (detached from mssql studio) using 'EF Designer from database' so I have my emdx model inside Models (asp.net mvc5) with classes for each table and DB MDF in App_Data.

Am I suppose to modify these classes?

Because I can add attributes right there but Diagram doesn't change and DB doesn't change. I guess I have to commit changes somehow.

I'll add that I can't enable migrations: Creating a DbModelBuilder or writing the EDMX from a DbContext created using Database First or Model First is not supported.

EDMX can only be obtained from a Code First DbContext created without using an existing DbCompiledModel.

5
  • Did you checked learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/… or stackoverflow.com/questions/25737054/…? If you want a field to be required, just add a not null constraint on this field. Commented Aug 29, 2018 at 13:01
  • It's not gonna work for DataType, tho. I can make changes in T-SQL but it's not really how I'd like to do this. I'd like to use EF. I saw people just making changes in classes referencing tables. How do I do that? I'm also open to changing approach if needed to code-first but without writing everything from scratch. Commented Aug 29, 2018 at 13:18
  • If you work database-first you only update the classes by updating the EDMX model. But DataType.Password isn't a supported data type in C# code, so that's not going to work. It's the trouble with all these data annotations: their role and effect are ambiguous because each framework uses their own subset for different purposes. Commented Aug 29, 2018 at 13:31
  • 1
    @GertArnold DataType.Password is used by MVC to generate <input type="password" /> if you let it scrap a view from a model. Commented Aug 29, 2018 at 13:33
  • 1
    DataType.Password work just fine, I saw that used a few days ago. It's not datatype, it's an attribute just like [Required], you use [DataType(DataType.Password)] Overall I changed the conception to use code-first approach but I used EF generate from existing database option so the output is classes based on my Tables inside mdf file. I just added proper Context and now I'm getting what I need, I think... When I compiled the asp.net app, the local DB changes but of course password is still varchar datatype with proper attribue in class. I don't have EDMX files at all now. Commented Aug 29, 2018 at 14:38

1 Answer 1

2

I think you are mixing allot of things here.

If you have an EDMX file, then your models are generated at compile time (or you can generate them from right click on the Model.tt file -> Run Custom Tool). So adding attributes to properties in a class representing a model entity will indeed be overwritten the next time you compile. The solution is:

  1. Create another partial class to the generated classes
  2. In the partial class, decorate the class with the [MetadataType] attribute and give it a type of a metadata class. The metadata class is a simple class, with the same properties as the generated class, but a different name, to prevent naming conflicts. From a design point of view, it should be abstract, because you're not supposed to create instances of it, but this is not required.
  3. In the metadata class, decorate the matching properties with the validation and DataType attributes.

To the best of my knowledge, using model-first or database-first doesn't support migrations as in code-first. If you want to make changes to your schema (semi) automatically, I believe your best option is:

  1. Make changes to your model in the EDMX designer
  2. Right-click on the EDMX design surface -> Generate Database from Model.
  3. After selecting the connection to your database, this will generate the SQL to generate your schema. This is a bit clunky, because it will erase your data each time, so you should have a script in place to re-populate your database after each time.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.