Database in use error with Entity Framework 4 Code First

Your current context must have an opened connection to be able to drop the database. The problem is that there can be other opened connections which will block your db initializer. One very nice example is having opened any table from your database in management studio. Another possible problem can be opened connections in the … Read more

One DbContext per request in ASP.NET MVC (without IOC container)

I know this is not a recent question, but I’ll post my answer anyway, because I believe someone may find it useful. As probably many others, I followed the steps mentioned in the accepted answer. Yay, it works. HOWEVER, there’s one catch: Methods BeginRequest() and EndRequest() fire each time a request is made, but not … Read more

Set decimal(16, 3) for a column in Code First Approach in EF4.3 [duplicate]

The DataType Attribute is a Validation Attribute. You need to do that using the ModelBuilder. public class MyContext : DbContext { public DbSet<MyClass> MyClass; protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<MyClass>().Property(x => x.SnachCount).HasPrecision(16, 3); modelBuilder.Entity<MyClass>().Property(x => x.MinimumStock).HasPrecision(16, 3); modelBuilder.Entity<MyClass>().Property(x => x.MaximumStock).HasPrecision(16, 3); } }

Get return value from stored procedure

I found it! I can read the return value with an output parameter that has to be used in this way: // define a new output parameter var returnCode = new SqlParameter(); returnCode.ParameterName = “@ReturnCode”; returnCode.SqlDbType = SqlDbType.Int; returnCode.Direction = ParameterDirection.Output; // assign the return code to the new output parameter and pass it to … Read more

Non-static method requires a target. Entity Framework 5 Code First

The problem boiled down to the query. My original question had this query: var allPartners = DbContext.User .Include(u => u.Businesses) .Where(u => u.Businesses.Any(x => x.Id == currentBusinessId)) .ToList(); Which wasn’t quite accurate, I had in fact removed the error in an attempt to ask my question succinctly. The query was actually: var currentBusiness = GetBusiness(); … Read more

How do I specify that a property should generate a TEXT column rather than an nvarchar(4000)

I appreciate the effort that went into the existing answer, but I haven’t found it actually answering the question… so I tested this, and found out that [Column(TypeName = “ntext”)] public string Body { get; set; } (the one from System.ComponentModel.DataAnnotations) will work to create an ntext type column. (My problem with the accepted answer … Read more

CodeFirst EF4.1 MVC Against legacy database – Multiplicity conflicts

Hope this is still on time to help you. I was also having the exact same problem and was troubling with it for almost an hour until I could spot my mistake. The problem is that Course.Venue relationship is optional (as declared on the fluent API), but the Id declaration of Course.VenueId is mandatory, so … Read more