entity-framework

unable to use custom database initializer

a 夏天 提交于 2020-03-21 19:30:35
问题 I am building a simple MVC application for managing a library. For development purposes, I would like the EF to drop and recreate the database everytime the model changes, as well as filling it with some sample data. At this moment I struggle at getting the initializer to work. The Initializer class looks like this: public class LibraryInitializer : DropCreateDatabaseIfModelChanges<LibraryContext> { protected override void Seed(LibraryContext context) { // sample data to be writted to the DB

The property X is of type Y which is not supported by current database provider

∥☆過路亽.° 提交于 2020-03-21 11:27:10
问题 I am really new to EF (using EF core 2.1) and have been following a bunch of tutorials so far, but now I have ventred in to creating my own DB structure and stumbled when trying to add a value in to the DB: private async Task<int> Insert() { var address = new Address { AddressLine1 = "1 any street", AddressLine2 = "", AddressLine3 = "", City = "Any city" }; using (var context = new BranchContext()) { context.Addresses.AddAsync(address);//ERROR HERE .... } } I get the error:

Define Entity Framework relationships using foreign keys only by FluentAPI

拜拜、爱过 提交于 2020-03-19 07:58:13
问题 Is any way of defining Entity Framework relations using foreign keys only (no virtual properties of reference type) with FluentAPI (data models should not be changed)? CardDataModel public class CardDataModel { public int CardId { get; set; } } CheckItemDataModel public class CheckItemDataModel { public int CheckItemId { get; set; } public int CardId { get; set; } } 回答1: Yes, it's possible in EF Core. It wasn't in EF6 and below, but now EF Core provides parameterless overloads of HasMany /

Return Entity Framework Objects as JSON

我们两清 提交于 2020-03-19 06:45:13
问题 I try to return Entity Framework Objects as Json with the following method in my Controller: public JsonResult EventList() { var results = from s in db.Events select new { OrderID = s.EventID, OrderTitle =s.EventType, OrderDate = s.Title }; return Json(results); } I get a server error 500 when entering the page /events/EventList/. Also a Jquery get request returns no data. What is the correct way to return the results in Json format? Update: This seems to work. But I need results from the

How to update an entity without a round-trip? (EF 4)

烈酒焚心 提交于 2020-03-18 12:12:33
问题 I tried the following: public void UpdatePlayer(int id) { Player player = new Player() {ID = id}; player.Password = "12"; Entities.Players.Attach(player); Entities.SaveChanges(); } No change at the db. What am I missing? 回答1: I think it might be because you're setting the values before you attach the object - the data context will not know what fields have changed. Try: public void UpdatePlayer(int id) { Player player = new Player() {ID = id}; Entities.Players.Attach(player); player.Password

Entity Framework 6 DbSet AddRange vs IDbSet Add - How Can AddRange be so much faster?

ⅰ亾dé卋堺 提交于 2020-03-18 11:27:33
问题 I was playing around with Entity Framework 6 on my home computer and decided to try out inserting a fairly large amount of rows, around 430k. My first try looked like this, yes I know it can be better but it was for research anyway: var watch = System.Diagnostics.Stopwatch.StartNew(); foreach (var event in group) { db.Events.Add(event); db.SaveChanges(); } var dbCount = db.Events.Count(x => x.ImportInformation.FileName == group.Key); if (dbCount != group.Count()) { throw new Exception(

Entity Framework 6 DbSet AddRange vs IDbSet Add - How Can AddRange be so much faster?

一曲冷凌霜 提交于 2020-03-18 11:27:10
问题 I was playing around with Entity Framework 6 on my home computer and decided to try out inserting a fairly large amount of rows, around 430k. My first try looked like this, yes I know it can be better but it was for research anyway: var watch = System.Diagnostics.Stopwatch.StartNew(); foreach (var event in group) { db.Events.Add(event); db.SaveChanges(); } var dbCount = db.Events.Count(x => x.ImportInformation.FileName == group.Key); if (dbCount != group.Count()) { throw new Exception(

Why does this EF insert with IDENTITY_INSERT not work?

ぃ、小莉子 提交于 2020-03-18 03:57:48
问题 This is the query: using (var db = new AppDbContext()) { var item = new IdentityItem {Id = 418, Name = "Abrahadabra" }; db.IdentityItems.Add(item); db.Database.ExecuteSqlCommand("SET IDENTITY_INSERT Test.Items ON;"); db.SaveChanges(); } When executed, the Id of the inserted record, on a new table, is still 1. NEW: When I use either the transaction, or TGlatzer's answer, I get the exception: Explicit value must be specified for identity column in table 'Items' either when IDENTITY_INSERT is

What's the simplest approach to friends list in EF?

本秂侑毒 提交于 2020-03-16 08:09:52
问题 I would like to create a simple friend list, with each row having two id's (user id and friend id), both referencing "id" field in respective user's profile |relationId|userId|friendId| So that users become friends if both sides send a friendship request to eachother. For example: |1|2|4| |1|4|2| //users with id 2 and 4 are friends Setting up a foreign key has been easy for me so far. Things get harder when I have to set up two foreign keys to the same table. I am getting different errors

What's the simplest approach to friends list in EF?

一曲冷凌霜 提交于 2020-03-16 08:08:13
问题 I would like to create a simple friend list, with each row having two id's (user id and friend id), both referencing "id" field in respective user's profile |relationId|userId|friendId| So that users become friends if both sides send a friendship request to eachother. For example: |1|2|4| |1|4|2| //users with id 2 and 4 are friends Setting up a foreign key has been easy for me so far. Things get harder when I have to set up two foreign keys to the same table. I am getting different errors