entity-framework-6

Incomplete EF code-first cascade delete on many to many relationship

那年仲夏 提交于 2019-12-11 05:51:33
问题 I have a PhoneNumber entity which I'd like to reference across multiple entities. For example, a Contact entity that has many PhoneNumbers, and a Business entity with one PhoneNumber. public class PhoneNumber { [Key, Column(Order = 0), DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public string Number { get; set; } } public class Contact { [Key, Column(Order = 0), DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public virtual

Entity Framework code-first, one-to-zero-to-one and one-to-many relationship to same entity

让人想犯罪 __ 提交于 2019-12-11 04:51:53
问题 I'm creating a code-first database with v6.0 of the Entity Framework. I have an Organisation class and a related Location class defined in c# as follows: public class Organisation { public int Id { get; set; } public string Name { get; set; } public virtual ICollection<Location> Locations { get; set; } public int? HQLocationId { get; set; } public Location HQLocation { get; set; } } public class Location { public int Id { get; set; } public string Name { get; set; } public int OrganisationId

Entity Framework left join multiple tables failure

让人想犯罪 __ 提交于 2019-12-11 04:43:42
问题 I'm using Entity Framework 6 and I have few entities and a query like the following: var results = (from e1 in dataContext.Entity1 .Where(x => x.Key1 == 1) from e2 in dataContext.Entity2 .Where(x => x.Key2 == e1.Key1) .DefaultIfEmpty() from e3 in dataContext.Entity3 .Where(x => x.Key3 == e1.Key1 || x.Key3 == e2.Key2) .DefaultIfEmpty() select new { E1 = e1, E2 = e2, E3 = e3 }).ToList(); Since the joins to Entity2 and Entity3 are left joins, e2 or e3 may be null. I found out if e2 is null,

Is there a way to force SortOrder on Related Data in Entity Framework?

断了今生、忘了曾经 提交于 2019-12-11 04:34:50
问题 Assuming that I have an EF Code-First implementation with the following simplified scenario: public class Form{ [key] int FormID {get;set;} int Name {get;set;} public virtual ICollection<FormAndQuestionMapping> FormQuestionMappings { get; set; } } public class FormAndQuestionMapping{ [ForeginKey] int FormID {get;set;} [ForeginKey] int QuestionID {get;set;} int SortOrder {get; set;} public virtual Form Form { get; set; } public virtual Question Question { get; set; } } FormAndQuestionMapping

DbContext requires manual load of navigation properties

两盒软妹~` 提交于 2019-12-11 04:19:47
问题 I recently upgraded my solution from EF5 to EF6.1.2, and changed my data access layer to use DbContext instead of ObjectContext. Some of my unit tests are failing, and I don't understand why. Example of old data access code: public virtual T Insert(T item) { if (item == null) { throw new ArgumentNullException("item", @"TaskDal.Insert"); } using (var ctx = ObjectContextManager<StoreDataContext>.GetManager("StoreDataContext")) { var task = new Task(); WriteNonKeyData(task, item); ctx

C# Entity Framework 6: Out Of Memory when it absolutely should not run out of memory

落花浮王杯 提交于 2019-12-11 04:13:23
问题 I've simplified things as much as possible. This is reading from a table that has around 3,000,000 rows. I want to create a Dictionary from some concatenated fields of the data. Here's the code that, in my opinion, should never, ever throw an Out Of Memory Exception: public int StupidFunction() { var context = GetContext(); int skip = 0; int take = 100000; var batch = context.VarsHG19.OrderBy(v => v.Id).Skip(skip).Take(take); while (batch.Any()) { batch.ToList(); skip += take; batch = context

Using Entity Framework 6 / EF Core inside SQL CLR

荒凉一梦 提交于 2019-12-11 04:08:55
问题 Microsoft Azure Tech support has confirmed that all version of .Net 4.XX including 4.7.2 is supported in SQL Server Managed Instance CLR. We're transforming some portion of Business layer with 4.7.2 and latest EF 6.2 Use Entity Framework in CLR Stored procedure https://patrickdesjardins.com/blog/how-to-use-third-party-dll-reference-in-a-sql-clr-function Therefore in order to leverage existing investment in well tested code we want to move certain business layer into DB. However we ran into

Entity Framework 6 DbUpdateConcurrencyException When Adding New Rows To Database

隐身守侯 提交于 2019-12-11 03:45:13
问题 Here's my problem: Every time I attempt to add a row to the database, I get this exception: DbUpdateConcurrencyException No one else uses this development database except me. Please help. I'm at my wits end here. Here's the code: using (var context = new MyDbContext(connectionString)) { context.Database.Log = s => System.Diagnostics.Debug.WriteLine(s); var newProduct = new ProductsEntity { ProductTypeId = 1 }; context.Products.Add(newProduct); // The line below always throws the exception

EF Code First CREATE DATABASE permission denied in database 'master'

天大地大妈咪最大 提交于 2019-12-11 03:23:55
问题 I used code first for create and update database. My update-database commands in package manager worked until yesterday. But today it is not working and has this error: CREATE DATABASE permission denied in database 'master'. I do not any changes on connection string and other config. So I try to Update-database to another project on my system and my windows and I can recreate and update this database without any problem. Can you help me for fix this error? This is my connection string for

Issue loading child entity of a parent entity. Unidirectional mapping and 1 to 0..1 relationship with a Shared primary Key?

纵饮孤独 提交于 2019-12-11 03:23:22
问题 When I try to load child entity of parent entity it loads with default values. If i try to load explicitly it throws exception Multiplicity constraint violated . The role 'Association_Customer_Target' of the relationship 'CodeFirstNamespace.Association_Customer' has multiplicity 1 or 0..1. This exception is thrown while retrieving the child entities of a complex graph. I have a graph Association which has a child entity Customer with a relationship of one to zero or one and has an Independent