Entity Framework, Existing Database, Code First - Ignoring Db Columns and Changing Datatypes

I'm using Entity Framework 4.3 (I'm open to suggestions on changing this if EF can't do what I need).

I have some Entities/Business Objects. These objects should not change, should not be annotated (no DataAnnotation Attributes), should not have internal members added for workarounds, and the database is also set in stone / beyond my control. In short, the DB cannot change, nor can my entities. That, at first, sounded like a perfect reason to use Fluent API.

public class MyEntity
    public int MyEntityId { get; set; }
    public string Name { get; set; }
    public bool IsGreen { get; set; }

Let's say the table to which this object relates in the database is as follows:

CREATE TABLE [dbo].[MyEntities]
ForeignKeyId INT NULL

apologies if the SQL is wrong, quickly hand typed

Hopefully you'll notice straight away a couple of conflicts.

Question 1) Is it possible, through Fluent API, to map my boolean to the IsGreen CHAR(1) NOT NULL field without adding a workaround property to the original entity? This column is not nullable, the expression to populate the bool would likely be along the lines of:

x => new MyEntity() { IsGreen = x.IsGreen == "Y" }

Question 2) Will the context allow me to Insert MyEntity to the database, despite not all columns being mapped, so long as the columns not mapped are nullable, and even if there is a foreign key?

Question 3) If EF cannot do the above, is there a framework that can?

Here's what I've tried so far:

Attempt 1: I started going down the road of creating seperate entities based on the DB Schema, then creating lengthy expressions to be able to convert from one to the other.

This may have eventually worked, but ignoring the ludicrous amount of time it was taking, it created a new issue; my Expression<Func<MyEntity, bool>> expressions for where clauses could obviously no longer be applied at the DB, instead having to bring all records back, compiling the expression, and applying it to a resulting enumerable.

This is not good enough unfortunately, there is simply too much data being brought back. I could have spent some time writing code that walked through the expressions and mapped the MemberAccess nodes to the new entity, but that seemed unnecessarily complex.

Attempt 2:

I then tried to create inherited classes, which could contain workaround properties, setting the actual properties in their setters, whilst the actual properties were ignored through the fluent API. Thanks to the inheritance, my aforementioned where expressions should then have worked.

However, the inheritance was discovered by EF, and I was no longer able to query the database due to the schema not matching the inheritance hierarchy of entities it seemed to be expecting. (I appreciate that's a little vague, but consider the TPH/TPT/etc aspects of EF, and how that wasn't necessarily matching my schema - I think this was my closest attempt but I didn't know how to correct the issues through the fluent API).

Attempt 3: Finally, I tried to tinker with the designer. Not much to say about that one, except that I'm no longer tinkering with the designer.

NB: this software needs to run against several different databases, those databases will not have exactly the same schema each time, which is why for example I need the code to work even though my model doesn't necessarily have a property for each column present in the DB. Not only that, but I do not know, before the software ships, how many or what extra columns the database may have, all I know is that the columns are nullable, and therefore I should be able to insert into the table knowing only the core non-null columns (which are those present on MyEntity objects). This alone, I believe, rules out the designer.

Sorry for the wall of text - any help appreciated!


Answer 1: No. EF currently doesn't support mapping char(1) to bool. You can vote for my suggestion.

Answer 2: Yes, if all non mapped columns are nullable or have default value constraint defined.

Answer 3: NHibernate

Need Your Help

mongodb: Retrieve last N records using C language

c mongodb

I am running one 'C' application with mongodb. Using mongo shell, I have found a way to retrieve last N records.

Periodic Tasks with Celery and Django

python django celery periodic-task

I'm having troubles getting a periodic tasks to run with Celery 3.1.8, Django 1.6.1, and RabbitMQ. I'm a bit confused with the current documentation as I understand that django-celery is not needed