entity-framework

Unit testing a two way EF relationship

ぃ、小莉子 提交于 2020-01-22 18:40:52
问题 I'm doing a small practice project to improve my unit testing skills. I'm using Entity Framework Code First. I'm using a FakeDBSet, which works well for simple lists of entities. When entity trees are returned things aren't so nice. In particular two way relationships aren't maintained as this is part of the Entity Framework magic. I have two classes: public class Book { public virtual ICollection<Review> Reviews {get; set;} } public class Review { public virtual Book Book { get; set;} } If I

The cast to value type 'Double' failed because the materialized value is null

巧了我就是萌 提交于 2020-01-22 17:04:21
问题 CODE: double cafeSales = db.InvoiceLines .Where(x => x.UserId == user.UserId && x.DateCharged >= dateStart && x.DateCharged <= dateEnd) .Sum(x => x.Quantity * x.Price); ERROR: The cast to value type 'Double' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type. WHAT I HAVE SEEN ALREADY: The cast to value type 'Int32' failed because the materialized value is null The cast to value type 'Decimal' failed because the

How to access many-to-many table via Entity Framework? asp.net

断了今生、忘了曾经 提交于 2020-01-22 16:49:52
问题 How do I read a many-to-many table via EF? I have no idea how to use the many-to-many table. Let's say Product_Category where it got ProductID and CategoryID . How can I access it trough e.g. using(Entities db = new Entities) { /* cant access these here.. */} method?? I can however reach Product_Category , but cant access its ProductID or CategoryID . I want to list every product e.g. where Product_Category.CategoryID == Category.ID . I have never used many-to-many tables before, so I

How to convert my EF Code-First to Database first?

独自空忆成欢 提交于 2020-01-22 15:15:50
问题 I want to convert my code-first project to Database first. Is there an automated way or should I just delete the entities and context code and create a model from the created database? 回答1: There is no way to convert your code-first classes into database-first classes. Creating the model from the database will create a whole new set of classes, regardless of the presence of your code-first classes. However, you might not want to delete your code-first classes right away. The entity framework

How to convert my EF Code-First to Database first?

喜夏-厌秋 提交于 2020-01-22 15:15:31
问题 I want to convert my code-first project to Database first. Is there an automated way or should I just delete the entities and context code and create a model from the created database? 回答1: There is no way to convert your code-first classes into database-first classes. Creating the model from the database will create a whole new set of classes, regardless of the presence of your code-first classes. However, you might not want to delete your code-first classes right away. The entity framework

How to convert my EF Code-First to Database first?

本小妞迷上赌 提交于 2020-01-22 15:15:26
问题 I want to convert my code-first project to Database first. Is there an automated way or should I just delete the entities and context code and create a model from the created database? 回答1: There is no way to convert your code-first classes into database-first classes. Creating the model from the database will create a whole new set of classes, regardless of the presence of your code-first classes. However, you might not want to delete your code-first classes right away. The entity framework

How to implement ambient transaction in Entity Framework Core?

你说的曾经没有我的故事 提交于 2020-01-22 15:15:08
问题 I need to realize transaction under two models (using two separated bounded contexts). So code like this: using (TransactionScope scope = new TransactionScope()) { //Operation 1 using(var context1 = new Context1()) { context1.Add(someCollection1); context1.SaveChanges(); } //Operation 2 using(var context2 = new Context2()) { context2.Add(someCollection2); context2.SaveChanges(); } scope.Complete(); } return the exception: An ambient transaction has been detected. Entity Framework Core does

How to implement ambient transaction in Entity Framework Core?

家住魔仙堡 提交于 2020-01-22 15:14:09
问题 I need to realize transaction under two models (using two separated bounded contexts). So code like this: using (TransactionScope scope = new TransactionScope()) { //Operation 1 using(var context1 = new Context1()) { context1.Add(someCollection1); context1.SaveChanges(); } //Operation 2 using(var context2 = new Context2()) { context2.Add(someCollection2); context2.SaveChanges(); } scope.Complete(); } return the exception: An ambient transaction has been detected. Entity Framework Core does

Entity Framework - relationship with fake Foreign Key (no foreign key in the db)

南楼画角 提交于 2020-01-22 14:14:28
问题 I have many tables that have TextID column which refers to the translation table. Translation table needs also LanguageID to get translated text in desired language. My problem is that I do not have LanguageID in my database, it is predefined in the system and I do not know how can I define it using Fluent API, i.e this can be my model: public partial class MyEntity { public short ID { get; set; } public Nullable<int> TextID { get; set; } [NotMapped] public Nullable<int> LanguageID { get; set

Entity Framework - relationship with fake Foreign Key (no foreign key in the db)

若如初见. 提交于 2020-01-22 14:13:45
问题 I have many tables that have TextID column which refers to the translation table. Translation table needs also LanguageID to get translated text in desired language. My problem is that I do not have LanguageID in my database, it is predefined in the system and I do not know how can I define it using Fluent API, i.e this can be my model: public partial class MyEntity { public short ID { get; set; } public Nullable<int> TextID { get; set; } [NotMapped] public Nullable<int> LanguageID { get; set