entity-framework-6.1

Entity framework update foreign key to null without query and without id of the related entity

一笑奈何 提交于 2019-12-12 03:25:19
问题 Using EF 6.1, I want to set a foreign key to null (break the relation between two entities), without querying for either object first. Preferably, I also don't want to supply the id of the related entity (since that requires adding it to my HTML). My current code doesn't do unnecessary queries, but requires the id of the related entity: public void RemoveCurrentTeacherOfGroup(int groupId, int teacherId) { var group = new Group { Id = groupId }; var teacher = new Teacher { Id = teacherId };

Entity Framework 6 (EF6) code first migrations with models in separate project

假如想象 提交于 2019-12-12 02:06:05
问题 Using EF6 code-first migrations, I am able to successfully save models and create new migrations for them. However, my DbContext class is in a Sharp.Data project, the actual (Sql CE) database lives under the Sharp.Server project bin folder, and my models live in a Sharp.Common project. When I run add-migration -ProjectName Sharp.Data Migration3 (pointing to Sharp.Data as that is where the DbContext is), it successfully runs and identifies changes made to the models in the Sharp.Common project

Insert instead of update for history data?

爱⌒轻易说出口 提交于 2019-12-11 20:58:17
问题 I am using EF 6.1 Code First and want to track history data by using a model which (simplified) looks like this: public class Customer : IHistoryData<CustomerData> { public Guid ID { get; set; } public virtual ISet<CustomerData> Versions { get; set; } } public class CustomerData : HistoricalData { public string Name { get; set; } } public interface IHistoryData<T> where T : HistoricalData { ISet<T> Versions { get; set; } } public class HistoricalData { public int VersionID { get; set; }

Entity Framework eager loads associated collection but won't lazy load it

被刻印的时光 ゝ 提交于 2019-12-11 19:08:30
问题 I've tried to get the Studies property of the Department object to lazy load, but it will only load when I eagerly load it. I've added a DepartmentId to the Study class with no results, used ICollection , ISet and virtual . Public and private setters seems to make no difference (and it shouldn't). Nothing seems to work. Using EF 6.1. public class Department { private Department() {} public Department(DepartmentTitle title) { if (title == null) throw new ArgumentNullException(); this.Title =

EF 6.1 Code First - the number of columns specified must match the number of primary key columns

故事扮演 提交于 2019-12-11 13:47:37
问题 I am running into the following error while trying to migrate a database in Entity Framework. The specified association foreign key columns 'question_set_id' are invalid. The number of columns specified must match the number of primary key columns. I dropped the original primary key QuestionSetId and created a composite key relationship. The columns in the composite key relationship also map to foreign keys. I'm not sure what the issue is. Here is the associated entity. public class

Entity framework slow to connect to database?

放肆的年华 提交于 2019-12-11 12:02:48
问题 var sw = new Stopwatch(); sw.Start(); Database.SetInitializer(new DropCreateDatabaseIfModelChanges<ImgSimContext>()); using (var db = new ImgSimContext()) { SqlConnection.ClearAllPools(); db.Database.Initialize(force: true); sw.Stop(); Console.WriteLine("Booted in {0}ms", sw.ElapsedMilliseconds); This takes about 2 seconds, even when the database has already been created. Is there any way I can speed it up? 回答1: If you are using EF6, then it is a known issue as described here: https:/

EntityFramework.Extensions 6.1 Batch Delete throws “Sequence contains more than one element”

折月煮酒 提交于 2019-12-11 09:04:32
问题 Trying to use EntityFramework.Extensions for Delete and I have a case where I get the error from the title. Here is the scenario: public class AList { [Key] public int Id { get; set; } [Column] public int XId { get; set; } } public abstract class X { [Key] public int Id { get; set; } public ICollection<AList> TheAList { get; set; } } public class Y : X { [Column("TheId")] public int? SomeId { get; set; } } public class Z : X { [Column("TheId")] public int? SomeIdZ { get; set; } } This is the

New subentity will not save when parent entity is saved

我们两清 提交于 2019-12-11 02:56:25
问题 I have a parent entity foo which exists in the db, I have a property bar on this entity (One to many relation). Foo is detached because its deserialized using WebApi, so I do this to foo context.Foos.AddOrUpdate(foo); Even If there is a new bar reference attached to it, it wont get saved, however it does work like this for another relation we have which is a Many to many relation. If I add a new entity to that collection it will be saved to its table and also a row is added to the relation

How can I suppress execution from an Entity Framework 6 IDbCommandTreeInterceptor?

本秂侑毒 提交于 2019-12-11 02:38:07
问题 I implemented the soft-delete pattern demonstrated by Rowan Miller during a TechEd session but I ran into an immediate problem because I am using inheritance in my Code First model. The first error was during queries because I put the IsDeleted property on my supertype (base class) but when I intercepted the query for a subtype and tried to add the filter, EF complained that there was no such property on that type. Fair enough, I moved the properties to the subtypes and that bit worked okay.

Entity Framework Code-First - Define the Non-clustered key for this EntityType

百般思念 提交于 2019-12-08 13:42:51
问题 I Have defined an entity class with a key, but I don,t want that key be cluster because I have another cluster index in my entity, but I received an error when I add an Index attribute to my model, Can anybody tell me that how can I define a Non-Clustered Key ???? 回答1: AFAIK it is still not possible to create non-clustered primary key fields as part of your code first configuration. However, if you are using migrations or are in a position where you are able to, see the following Entity