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.

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the shared link! I think metadata class is a perfect solution.
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

4 Comments

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.
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.
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.
Thanks ninia coder, your reply exactly solve my question.
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;
}

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.