1

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.

Andrew Li
58.1k14 gold badges135 silver badges150 bronze badges
asked Dec 19, 2016 at 23:22
0

3 Answers 3

2

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

answered Dec 19, 2016 at 23:37
1
  • Thanks for the shared link! I think metadata class is a perfect solution. Commented Dec 19, 2016 at 23:57
1

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

answered Dec 20, 2016 at 1:07
4
  • 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. Commented 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. Commented 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. Commented Dec 20, 2016 at 3:20
  • Thanks ninia coder, your reply exactly solve my question. Commented Dec 20, 2016 at 3:24
0

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;
}
answered Dec 20, 2016 at 14:26

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.