简体   繁体   中英

Database First Entity Framework

I am developing a web-based system using .NET MVC and Entity Framework, from a legacy system. I will keep the legacy database and only develop the application, so I will create entity classes using database first approach.

My concern is, in the entity classes, I will be adding attributes such as filter attributes. And I may add some extra fields there as well. Will the database be updated automatically? I actually don't want to update the database at all. I only want extra stuff stay with the entity class only.

You can easily create your database first and then start working on the backend side of your application as you would do it in an "Code first" approach. I'd recommend reading this article, as you might get all your answers cleared with some examples: Entity framework

First, when adding additional properties in your entity classes you will want to mark them for EF to ignore using either the [NotMapped] attribute or in your DbContext class calling .Ignore on the property. Not Mapped Attribute

Second, in terms of EF changing your database you can do a couple of things. 1. Ensure the account that is updating data does not have Create,Alter,etc permissions in the environment 2. Use a null database initializer at the beginning of your application. Database.SetInitializer(null); Setting the initializer to null tells EF not to make any sort of changes to the database. Turn off DB Initializer

Maybe you should consider using partial classes. I find editing anything in EF quite hard.

EF Classes from your DB are all :

public partial myClass(){
    public string myDBProperty;
}

Which means you are able to create a second class in the same namespace like :

public partial myClass(){
    public string myOwnProperty;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM