by TheRealDunkirk · 12y ago · 0 comments
I'm typing this to delay the effort of ripping EF out of my project, and do ADO.NET Linq-to-SQL. (I guess. Maybe it'll just be raw SQL at this point.) Unless someone here can answer this question? It's worth a shot... http://stackoverflow.com/questions/23528335/how-can-i-implem...
I miss Rails.
Here's the original post describing how we made it: http://samsaffron.com/archive/2011/03/30/How+I+learned+to+st...
Also known as "mapping"; the literal purpose of an ORM. :)
Other big tools/frameworks include APIs for query generation, unit of work, caching, etc., which are not always necessary or desired.
ORM's compensate for the impedance mismatch between objects and tables. They do a lot more than mapping.
First add foreign keys to to models (e.g. In the Build class add MainlineID for the Mainline object).
Then you can define the configuration:
Public Class BuildConfiguration
Implements EntityTypeConfiguration(Of Build)
Public Sub New()
ToTable("Build")
' Other mappings
' Define one-to-many
HasRequired(Function(x) x.Mainline) _
.WithMany(Function(x) x.Builds) _
.HasForeignKey(Function(x) x.MainlineID)
End Sub
End Class
Inside your context you load the configuration: Public Class MyContext
Inherits DbContext
Public Property Builds As DbSet(Of Build)
Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
MyBase.OnModelCreating(modelBuilder)
modelBuilder.Configurations.Add(New BuildConfiguration())
End Sub
End Class
Then you can use the Include extension method (or eager loading) on the DbContext to populate the object. Using context As New MyContext()
Return context.Builder.Include(Function(b) b.Mainline)
End Using
See also:http://weblogs.asp.net/manavi/archive/2011/05/17/association...
var releases = db.Releases.Where(r => r.Build.Mainline.MainlineID == 4);
Or am I missing something?EF has its fair share of issues and limitations, but that's not one of them.
The modern C# stack can be very pleasant, but it's in an ocean of horrible mistakes and dead-ends that they can't flag as such for marketing reasons.
.NET 4 made EF good, but 3.5 was a mess.
Although many use nhibernate instead of EF, i have no experience with it... (it's more db agnostic though).
I'm not sure, but i wouldn't advice Linq To SQL though...
In the end, we decided to continue to maintain and enhance our own ORM, because of the special features it has to support the Enterprise niches we work with. But if we had to start over fresh today, we'd definitely avoid EF and we'd probably build upon NHibernate. As far as we could tell, NHibernate had the most mature features and ability to support the market we're in.