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.
3 Answers 3
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
-
Thanks for the shared link! I think metadata class is a perfect solution.DennisL– DennisL2016年12月19日 23:57:55 +00:00Commented Dec 19, 2016 at 23:57
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
-
Thanks for the reply. When I said I don't want the EF change the database, I actually mean not to change the database structure. But it's perfectly okay if the EF insert record entries into the database.DennisL– DennisL2016年12月20日 03:15:37 +00:00Commented Dec 20, 2016 at 3:15
-
For example, if I add extra fields or relationship to the entity classes, I want these new things stay with the classes only. I don't want the entity classes to update my database table automactially.DennisL– DennisL2016年12月20日 03:18:55 +00:00Commented Dec 20, 2016 at 3:18
-
I am working with a database still linking to a legacy system. So I just want to be cautious, and not to change the database structure at all.DennisL– DennisL2016年12月20日 03:20:44 +00:00Commented Dec 20, 2016 at 3:20
-
Thanks ninia coder, your reply exactly solve my question.DennisL– DennisL2016年12月20日 03:24:57 +00:00Commented Dec 20, 2016 at 3:24
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;
}
Explore related questions
See similar questions with these tags.