If a database table in Entity Framework 6 has a column that does not exist in the entity, can we guarantee that the column will just be ignored and will not negatively impact processing?

Takayoshi Enomoto 40 Reputation points
2024-04-10T08:06:44.47+00:00

Is there any official document that can guarantee that adding a column to a SQL Server table referenced by EF6 in an existing system will not affect the system? 

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,860 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,822 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Jiachen Li-MSFT 31,331 Reputation points Microsoft Vendor
    2024-04-10T12:25:03.4833333+00:00

    Hi @Takayoshi Enomoto ,

    If you don’t make changes to the entity class (i.e., you don’t add any new properties), Entity Framework 6 (EF6) will ignore any new columns that exist in the database but are not represented in the entity.

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Bruce (SqlWork.com) 65,316 Reputation points
    2024-04-10T16:28:46.57+00:00

    But if the column does not allow null, or does not have a default value, EF inserts will fail.


  3. Michael Taylor 54,321 Reputation points
    2024-04-10T16:35:42.7133333+00:00

    In fact, if you're following the DDD concept of bounded contexts then you really shouldn't be adding any columns to your entities that your app doesn't actually need. It wastes time loading and saving the entity (creating a proxy, state management, etc) and clutters your code. Furthermore if you eventually want to get rid of that column then you have to open your app to remove the reference even though the app never used it to begin with.

    Of course if the column is required and doesn't have a default value then you have to insert a value into it and therefore EF has to know about it. But that would be a breaking change to any app that inserts values into the table, irrelevant of EF.


  4. SurferOnWww 2,896 Reputation points
    2024-04-11T01:11:50.3666667+00:00

    Is there any official document that can guarantee that adding a column to a SQL Server table referenced by EF6 in an existing system will not affect the system?

    Are you saying that you have an existing application which uses EF6 to do some operation of the existing database table and you want to add a filed to the existing database table?

    If so, I believe that there is no such official document. It is not possible for anybody to guarantee since nobody knows your details (i.e., schema of table, added field, usage of table in your application).

    Can you tell us the details? For example (only example though, please write your case),

    (1) Entity Data Model was created using the Visual Studio based on the Microsoft sample database Northwind (probably everybody knows its schema):

    edm

    (2) Controller and views of ASP.NET MVC5 were created for CRUD operations of the Products table using ASP.NET scaffolding available in Visual studio.

    (3) You want add field [Remark], nvarchar(255), NOT NULL.

    (4) Your question: above (3) affects the CRUD operation of the MVC5 application?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.