entity-framework-5

EF 5 AddOrUpdate Duplicates data

余生颓废 提交于 2019-11-30 17:37:46
问题 This is the code in the Seed method: var city = new City { Name = "A" }; var nh = new List<Neigh> { new Neigh { City = city, Name = "N1" }, new Neigh { City = city, Name = "N2" }, new Neigh { City = city, Name = "N3" }, //new Neigh { City = city, Name = "N4" }, }; context.Neighs.AddOrUpdate( p => p.Name, nh.ToArray() ); After running update-database everything works as expected. I can run it multiple times without problem. However if at some point I uncomment the fourth neighborhood and run

Create fulltext index within Entity Framework Coded Migrations

依然范特西╮ 提交于 2019-11-30 17:37:41
TLDR; How do you add a full text index using Entity framework 5 coded migrations I'm having issues adding a full text index to a database using Entity framework migrations. It needs to be there from the start so I'm attempting modifying the InitialCreate migration that was automatically generated to add it. As there isn't a way to do it via the DbMigrations API I've resorted to running inline sql at the end of the 'Up' code. Sql("create fulltext catalog AppNameCatalog;"); Sql("create fulltext index on Document (Data type column Extension) key index [PK_dbo.Document] on AppNameCatalog;"); When

Error modifying DAL, System.ArgumentException, “An entry with the same key already exist”

最后都变了- 提交于 2019-11-30 17:20:44
问题 OK, Im totally stumped with this one. I may not have enough info to post here, but I dont even know where to start looking. I'm trying to "Update Model from database" on my DAL.edmx file. I included a field to a view that wasnt included prior. I tried refreshing, and then I tried renaming the view in the database and deleting the view from the DAL so I could re-add it. Both times I got Next, for no reason I tried adding my renamed view into the DAL, got same exception. Manually deleting from

EF5 Migrations - Duplicate/Re-defined variable bug when dropping constraints | Issue with usage of SQL GO command

喜你入骨 提交于 2019-11-30 17:17:42
Background: We have a project that uses ef-migrations containing multiple (read ~60) migrations created over a long period of development. Naturally, some of these migrations also involve: dropping constraints 1 , 2 creating triggers All is unicorns and rainbows when we run Update-Database because each migration runs as a separate batch. But when creating SQL Scripts for these migrations using Update-Database -Script we encountered a few issues as described below: Problem 1: When dropping multiple constraints across multiple migration files, the script generated by EF tends to re-declare

AddOrUpdate works not as expected and produces duplicates

拜拜、爱过 提交于 2019-11-30 17:12:05
I'm using Code-First DBContext-based EF5 setup. In DbMigrationsConfiguration.Seed I'm trying to fill DB with default dummy data. To accomplish this task, I use DbSet.AddOrUpdate method. The simplest code to illustrate my aim: j = 0; var cities = new[] { "Berlin", "Vienna", "London", "Bristol", "Rome", "Stockholm", "Oslo", "Helsinki", "Amsterdam", "Dublin" }; var cityObjects = new City[cities.Length]; foreach (string c in cities) { int id = r.NextDouble() > 0.5 ? 0 : 1; var city = new City { Id = j, Name = c, Slug = c.ToLowerInvariant(), Region = regions[id], RegionId = regions[id].Id, Reviewed

Entity Framework 5 expects CreatedOn column from MigrationHistory table

自闭症网瘾萝莉.ら 提交于 2019-11-30 17:10:46
I am migrating an MVC 3 application from EF 4.3 to EF 5. I noticed that EF 5 expects a CreatedOn column in the __MigrationHistory table, which does not exist as the migrations were created by an older version. SELECT TOP (1) [c].[CreatedOn] AS [CreatedOn] FROM [dbo].[__MigrationHistory] AS [c] How do I resolve this issue without wiping my migration history? I am thinking of a query to infer the column's value from the migration name, which is in the following format: 201203111201542_MigrationName The CreatedOn column is no longer required. We attempt to query from it in order to determine

Why am I getting an IdentityRole error?

时光毁灭记忆、已成空白 提交于 2019-11-30 16:03:31
Using Identity 2.0 After registering a user, the user is created in the database, there is code in the Account controller to create an identity and sign the user in. This is when I get the error. Here is the code producing the error: var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); And here is the error: The entity type IdentityRole is not part of the model for the current context. My Model looks like this: namespace MyApp.Models { public class ApplicationUser : IdentityUser { [Required] [StringLength(50)] public string FirstName { get;

How to define nested Identifying Relationships Entity Framework code first

六眼飞鱼酱① 提交于 2019-11-30 15:43:16
问题 I'm using EF5 code first in a simple test application at the moment to test various functions. I have defined an 'identifying relationship' between two entities which represent a one-to-many link. Here I define a PhotoCollection that has many child Photo entities; public class PhotoCollection { public int Id { get; set; } public virtual ISet<Photo> Photos { get; private set; } public PhotoCollection() { Photos = new HashSet<Photo>(); } } public class Photo { [Key, ForeignKey("Parent"), Column

How to define nested Identifying Relationships Entity Framework code first

馋奶兔 提交于 2019-11-30 14:53:05
I'm using EF5 code first in a simple test application at the moment to test various functions. I have defined an 'identifying relationship' between two entities which represent a one-to-many link. Here I define a PhotoCollection that has many child Photo entities; public class PhotoCollection { public int Id { get; set; } public virtual ISet<Photo> Photos { get; private set; } public PhotoCollection() { Photos = new HashSet<Photo>(); } } public class Photo { [Key, ForeignKey("Parent"), Column(Order = 1)] public int PhotoCollectionId { get; set; } [Key, Column(Order = 2)] public int PhotoId {

Can I continue using DbContext after it has thrown an exception?

喜你入骨 提交于 2019-11-30 13:51:33
问题 I'm aware of two different scenarios in which exceptions can be produced when working with an Entity Framework DbContext: Enumerating a query (could throw a EntityCommandExecutionException) Calling SaveChanges (could throw a DbUpdateException) Within a single instance of DbContext, I'm wanting to catch these exceptions, try to recover if applicable, and then repeat the operation. Specifically, if a call to SaveChanges throws an exception because of a deadlock, I would like to retry the call