Entity Framework - Set Table Field like Set Property

Suamere

Member
Joined
Aug 23, 2012
Messages
11
Programming Experience
5-10
When using Entity Framework, you create the database like an entity in your project. You can then do things more or less like:

Database.Table.Column = Value
SaveChanges

The assignment is direct, though. Opposed to this:

Property pValue As String
Get
End Get
Set(ByVal value as String)
If value = "something" _pValue = "something"
End Set
End Property

I want to be able to apply behind the scenes logic with database fields like properties, without having to hand-type 100 properties into a class as a middle-man to a database. The goal is to allow a person to add any database to the project and to have it work, or to make changes to their database and just require an Entity Framework "update edmx" or whatever and not have to change the class.
 
Firstly, you're completely mistaken about how those EF classes are defined. They absolutely DO NOT use public fields. They all use properties, just as any decent .NET code does. All the generated code is right there in your project so you are free to look at it if you want to confirm it for yourself. Just don't edit it.

As for the second part, an EF model is specific to a database schema. The EF works in one of two ways: either it generates entity classes from a database that you design or else it generates a database from entity classes that you design. Either way, the entity classes and the database are coupled. You can swap databases but they must be the same type of database (e.g. you can't use an Access database in place of SQL Server) and they must have the same schema.

If you want to make changes to your database then you must regenerate your EF model in your project, recompile the application and then redeploy it. If you have decoupled your data access layer from your application then that might be as simple as replacing just the one DLL. That said, if you have made changes to your database then surely your application must have changed too, so surely you need to redeploy the whole thing.

If you want to create a database-agnostic data access layer then the EF is not for you. For an example of how you might go about something like that, follow the Blog link in my signature and check out my post on the subject.
 
Back
Top