entity-framework-5

Getting 'Context is not constructible. Add a default constructor or provide an implementation of IDbContextFactory.\"

旧城冷巷雨未停 提交于 2019-11-29 13:30:19
I am getting this error when I try to use code first migrations. My context has a constructor with the connection name. public class VeraContext : DbContext, IDbContext { public VeraContext(string NameOrConnectionStringName = "VeraDB") : base(NameOrConnectionStringName) { } public IDbSet<User> Users { get; set; } public IDbSet<Product> Products { get; set; } public IDbSet<IntCat> IntCats { get; set; } } This connection name is injected with ninject when the project runs, I have also specified it as a default as in the above code but this did not help. kernel.Bind<IDbContext>() .To<VeraContext>

'Enable-Migrations' fails after upgrading to .NET 4.5 and EF 5

假装没事ソ 提交于 2019-11-29 13:29:55
I just upgraded my MVC4 project to .NET 4.5 and EF5 and started using VS2012. After realizing I needed to set-up auto-migrations in the Package Manager again I ran Enable-Migrations - EnableAutomaticMigrations and received the error No context type was found in the assembly 'MySolutionName'. Some Research has said that it has to do with EF5 not enabling prereleases. I ran Install-Package EntityFramework -IncludePrerelease but it said EF5 was already installed (which it was when I installed it through the NuGetmanager earlier without specifying -IncludePrerelease. Does anyone know what I have

Entity Framework 5 model first - Where is IDisposable gone?

浪子不回头ぞ 提交于 2019-11-29 13:12:58
In Entity Framework 5 model first, there seem to be some breaking changes due to the way the class files are generated (No more code generation, but T4 templates) 2 examples: The generated context file doesn't implement IDisposable anymore There isn't a constructor which takes a connectionstring anymore Are there more breaking changes? And what is the solution to them? The default code generated from a model in Entity Framework 5 now inherits DbContext instead of ObjectContext. This still implements IDisposable, but if you're getting an error from a line of code similar to this: using (var

Is it possible to set a unique constraint using Entity Framework Code First?

夙愿已清 提交于 2019-11-29 13:07:04
I want to enforce Unique constraint in a table & I am using Entity Framework Code-First. Is it possible to add a unique constraint using EF 6 as i believe in earlier versions it was not possible. It appears that the unique constraint feature that was scheduled to release with Version 6 got pushed to 6.1. With EF 6.1, you can define a constraint using the Index attribute as shown below: [Index("IX_FirstAndSecond", 1, IsUnique = true)] public int FirstColumn { get; set; } [Index("IX_FirstAndSecond", 2, IsUnique = true)] public int SecondColumn { get; set; } OR You can use Fluent API as shown

Fluent API - one to many

醉酒当歌 提交于 2019-11-29 12:03:48
问题 I cannot seem to find a one-to-many relationship syntax in the fluent API. As an example I have two tables as below User Id Name UserHistory Id UserId Date In the classes I have the following public class User { public int Id { get; set; } public virtual ICollection<UserHistory> Histories { get; set; } } public class UserHistory { public int Id { get; set; } public int UserId { get; set; } public DateTime Date { get; set; } } I have tried the following but I am not sure if its actually

Programmatic data transformation in EF5 Code First migration

萝らか妹 提交于 2019-11-29 11:52:58
Is it possible to do any type of programmatic data transformation in Entity Framework 5 Code First migrations? There is an Sql () method to execute queries, but it has return type void and I don't see any way to get the results of the queries I perform. Example I have table Recipe with one-to-many relationship to Ingredient . For various reasons I want to convert this to a Ingredients JSON string property instead. The only approach I can think of is something like this: Create new column IngredientsJson For each recipe, query its ingredients, construct a JSON string programmatically and insert

EntityFramework 5 filter an included navigation property

被刻印的时光 ゝ 提交于 2019-11-29 11:46:12
问题 I would like to find a way using Linq to filter a navigation property to a subset of related entities. I know all answers around this subject suggest doing an anonymous selector such as: query.Where(x => x.Users.Any(y => y.ID == actingUser.ID)) .Select(x => new { Event = x, Discussions = x.Discussions.Where(actingUser.GenerateSecurityFilterFor<Domain.Discussion>()) }) .OrderBy(x => x.Discussions.Count()) .ThenBy(x => x.Event.Name); However, this is significantly less than ideal due to the

EF5 Code First Migrations: “Column names in each table must be unique” error after using RenameColumn

本秂侑毒 提交于 2019-11-29 11:31:55
问题 We're using Entity Framework 5.0 Code First and Automatic Migrations. I had a class like so: public class TraversalZones { public int Low { get; set; } public int High { get; set; } }​ Then we realized these properties weren't really the right names, so we changed them: public class TraversalZones { public int Left { get; set; } public int Top { get; set; } }​ The rename refactored properly throughout the project, but I know Automatic Migrations aren't smart enough to pick up these explicit

EF connection string as DbContext constructor argument

北城以北 提交于 2019-11-29 10:25:56
I have seen some code sample that put an entity framework connection string as a constructor argument when creating a new DbContext. But when I added a new ADO.NET entity data model into a project (database first), the DbContext only has one parameterless constructor. Did I miss a step? What should I do to get that constructor? Visual Studio 2012 targeting .net framework 4.5 entity framework 5. As per Arthur Vickers' suggestion, I am extending the partial class to have a constructor that accepts connection string. In C# (very similar to hege's answer): public partial class MyEFEntities {

EF code first - custom foreign key constraint name

99封情书 提交于 2019-11-29 09:21:08
I wonder if there is any chance to change name of foreign key constraint generated by Entity Framework when using code first. I have two entities - User and Group - with many-to-many relationship so there is an association table GroupUser . Unfortunately, auto-generated foreign key constraints are named FK_dbo.GroupUser_dbo.User_User_UserId and FK_dbo.GroupUser_dbo.Group_Group_GroupId . I would like to have foreign key constraints called like FK_GroupUser_UserId and FK_GroupUser_GroupId . That looks much cleaner to me. It's not possible to customize the foreign key constraint name with data