views:

58

answers:

2

I'm having a great deal of trouble with the Entity Framework (4.0) due to the pattern of 'adding' to a program. (Using ASP.NET MVC)

The problem is that it feels like the entire structure of the Entity Framework is designed to 'build everything all at once'. It doesn't seem to lend itself to progressive development, because of all of the changes that go through with the tools. I.E if I go change my database, then it fubars up the models - and trying to do updates causes all kinds of havoc and chaos. This wouldn't be bad if all I had to do was re-drag everything and my models worked verbatim, but I have to make manual edits to fine tune things ...and it is getting repetitive, and error-prone.

Does anyone know of any better tools or methods for this kind of problem?

+2  A: 

Ok, take two. It sounds like instead of using the designer you would be better off going the code-first route and making use of data annotations.

Data Annotations: http://blogs.msdn.com/b/efdesign/archive/2010/03/30/data-annotations-in-the-entity-framework-and-code-first.aspx -- Check out the storeNameAttribute for the specific case you mentioned in the comment to my deleted answer

Good code-first writeup here: http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx

I'm just starting to dig into this myself but I've got some small samples up and running so if you have specific questions let me know and I'll try to answer.

kekekela
New code first was just released a couple of days ago:http://www.hanselman.com/blog/SimpleCodeFirstWithEntityFramework4MagicUnicornFeatureCTP4.aspx
jfar
Yeah, its the same ctp referenced in the ScottGu link I posted.
kekekela
Oh, oops, didn't see you posted the same link.
jfar
Yeah, I am looking at this. But it seems more for 'creating' the database; Which is not what we need to do. We just need to map using code first. But none of these examples really explain that.... at all.
Stacey
No, it works with a pre-existing database as well. The main thrust is that its a way of removing the designer and code-gen from the equation.
kekekela
These examples are extremely frustrating... it's like they just don't care about citing anything but the same thing three times over. Is there any place where I can explicitly just TELL it to use an existing database? That's all I want. I don't want it to be creating mine.
Stacey
By default it won't create a new one unless the one you specified doesn't exist.
kekekela
Where do I specify one? That's what is driving me crazy. There is no method or constructor that I can find that accepts any way to specify what database to use.
Stacey
ConnectionStrings in your config file, by default it'll look for the connectionString with the same name as your DbContext.
kekekela
Thank You! Why can none of these people writing samples take the ten seconds to just outright state that? It seems like such an overwhelmingly important thing to note. Is there any way to tell it a SPECIFIC connection string to look at?
Stacey
Is there any way to specifically tell it never to try to 'create' the database?
Stacey
@Stacey Ever try reading the MSDN documentation? http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.objectcontext.aspx
jfar
Yes, except ObjectContext is not being used here, so the documentation for it is generally useless, and there isn't an entry for DbContext that I've been able to find.
Stacey
"Is there any way to tell it a SPECIFIC connection string to look at?" Uggh, just realized that the DataAnnotations attribute that I thought could handle this is just in the *proposed* phase right now. Really a little surprised they wouldn't build this capability in right from the start, doing some digging now to try to figure out a way to make this work (I think the EF fluent api makes it possible but it looks cumbersome, trying to figure out some config driven method)
kekekela
I've been doing some work and I am having a bit more mild success. I am relatively ambivalent about this whole method; It feels too much like they are trying to accomplish the wrong thing with the 'fluent' code model.
Stacey
@Stacey - Some more on-point stuff got posted today... http://weblogs.asp.net/scottgu/archive/2010/07/23/entity-framework-4-code-first-custom-database-schema-mapping.aspx
kekekela
Ah, apparently you can pass a string into the context constructor to tell it what name to look for in app.config
kekekela
A: 

What did you do before? Did you refactor the database and NOT have to change code?

So far I've used everything from ADO.NET to 3 .NET ORMs ( Nhibernate, Linq2Sql, and Entity Framework ), dabbled in Django and Python, and now hobbying in Ruby on Rails. None of these tools completly solved the problem of the impedance mismatch between the database and the code.

Someplace somewhere this code has to exist:

DBCOLUMN => CODE //or
myModel.Property = rdr["something"]  //or
<Property Name="EndDate" Type="datetime" /> //or
Id(x => x.Id);

Whether its in a Rails View, Migration file, Linq2Sql designer, or Fluent Nhibernate file doesn't matter. It always going to be an issue.

My suggestion is to try and make the mismatch as small as possible. Don't rename badly named columns in the designer and take every step to make the conventions between your database and code the same.

The other answerer provided tools that simply change where the renames occur but in reality you're always going to have to do this sort of work.

jfar