entity-framework-5

DbContext has been disposed and autofac

允我心安 提交于 2019-11-28 06:33:36
I have a controller: private readonly ILogger _logger; private readonly IRepository _repository; public HomeController(ILogger logger, IRepository repository) { _logger = logger; _repository = repository; } This is the repository: public class EfRepository : IRepository { // ...methods for add, delete, update entities // .... public void Dispose() { if (this._context != null) { this._context.SaveChanges(); (this._context as IDisposable).Dispose(); this._context = null; } } } Finally, registration types in IoC: _builder.RegisterType<Logger>().As<ILogger>(); _builder.RegisterType<EfRepository>()

Check if there are any pending changes to be saved

耗尽温柔 提交于 2019-11-28 06:07:56
Is there a way to find out whether there are unsaved changes in my entity context, in the Entity Framework? This might work (if by changes you mean added, removed and modified entities): bool changesMade = (context.ObjectStateManager.GetObjectStateEntries(EntityState.Added).Count() + context.ObjectStateManager.GetObjectStateEntries(EntityState.Deleted).Count() + context.ObjectStateManager.GetObjectStateEntries(EntityState.Modified).Count() ) > 0; Edit: Improved code: bool changesMade = context. ObjectStateManager. GetObjectStateEntries(EntityState.Added | EntityState.Deleted | EntityState

Using MVC 4 SimpleMembership with an existing database-first EF model

风格不统一 提交于 2019-11-28 05:04:38
I am trying to use SimpleMembership in my MVC 4 for the first time and I already have an existing database and EF5 model created based on it! I searched a lot but I cant find how I could use it in my case and also to have everything under my own model. It would be great if somebody can give me an idea how to do this. Thanks Purely as a point of reference, it might be a good idea to create a new Internet Application template of an ASP.NET MVC 4 Web Application project (i.e. via File > New Project). If you look at the AccountController , as @zms6445 says, it is decorated with an

Programmatic data transformation in EF5 Code First migration

陌路散爱 提交于 2019-11-28 05:02:56
问题 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

EF Code First - creating database - Login failed for user

老子叫甜甜 提交于 2019-11-28 03:40:15
问题 I originally had a EF code first set up that was connecting to an existing database. This was working fine. I then made a couple changes to a POCO and decided to have code first generate the new database for me. Getting error: Cannot open database \"MyDatabase\" requested by the login. The login failed.\r\nLogin failed for user 'DOMAIN\username'. I deleted the old database, but I did not change the connection string: <add name="MyDatabaseContext" connectionString="Data Source=localhost

EF connection string as DbContext constructor argument

一笑奈何 提交于 2019-11-28 03:36:33
问题 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. 回答1: As per Arthur Vickers' suggestion, I am extending the partial class to have a constructor that

Debugging Package Manager Console Update-Database Seed Method

落花浮王杯 提交于 2019-11-28 03:17:19
I wanted to debug the Seed() method in my Entity Framework database configuration class when I run Update-Database from the Package Manager Console but didn't know how to do it. I wanted to share the solution with others in case they have the same issue. EthR Here is similar question with a solution that works really well. It does NOT require Thread.Sleep . Just Launches the debugger using this code. Clipped from the answer if (!System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Launch(); The way I solved this was to open a new instance of Visual Studio and then open the same

EF code first - custom foreign key constraint name

▼魔方 西西 提交于 2019-11-28 02:48:09
问题 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 .

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

℡╲_俬逩灬. 提交于 2019-11-28 02:24:10
问题 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. 回答1: 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

Entity Framework Multiple Column as Primary Key by Fluent Api

僤鯓⒐⒋嵵緔 提交于 2019-11-28 02:23:36
问题 These are my simplified domain classes. public class ProductCategory { public int ProductId { get; set; } public int CategoryId { get; set; } public virtual Product Product { get; set; } public virtual Category Category { get; set; } } public class Product { public int Id { get; set; } public string Name { get; set; } } public class Category { public int Id { get; set; } public string Name { get; set; } public int? ParentCategoryId { get; set;} } This is my mapping class. But it doesnt work.