entity-framework

Code First: Entity accessible in tests, but “not set to an instance of an object” outside of tests

我是研究僧i 提交于 2021-02-11 12:41:00
问题 I created an EF Code First Entity for a view in my database. I setup a test, and the access code works in my test, but when I run the code outside of the test, it throws "Object Reference not set to an instance of an object". I am stuck, as I cannot understand what could be causing the issue. Any ideas on why it's throwing an error? Here is my entity class: using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema;

Code First: Entity accessible in tests, but “not set to an instance of an object” outside of tests

◇◆丶佛笑我妖孽 提交于 2021-02-11 12:40:58
问题 I created an EF Code First Entity for a view in my database. I setup a test, and the access code works in my test, but when I run the code outside of the test, it throws "Object Reference not set to an instance of an object". I am stuck, as I cannot understand what could be causing the issue. Any ideas on why it's throwing an error? Here is my entity class: using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema;

WCF service with entity framework(relationship between table)

时光总嘲笑我的痴心妄想 提交于 2021-02-11 12:32:18
问题 I am creating a server on a WCF server. I have a database in which there are several tables. For each of the tables, you need to create a WCF service. The WCF service has two files, an interface where I implement my REST architecture and a microwave file where I implement the methods that I call on this interface. When working with the database, EF, database first was used. This means that the data was imported from MySQL by the framework itself and automatically established connections. 1st

EF LINQ include nested entities [duplicate]

元气小坏坏 提交于 2021-02-11 12:03:28
问题 This question already has an answer here : Tree stucture table and entity framework (1 answer) Closed 3 years ago . I have multi-leveled entities with the following hierarchy: Parent-> RootChildren -> Children -> Children -> ..... Class Parent { public int Id {get; set;} public virtual Child RootChildren {get; set;} } Class Child { public virtual List<Child> Children {get; set;} } Now, I want to include entity which include all the child as nested way. I tried the following but it didn't work

How to cast UserStore<IdentityUser> to its base class IUserStore<IUser>?

寵の児 提交于 2021-02-11 07:10:37
问题 I am trying to use the new asp.net identity provider with my abstraction layer of my models domain, wich have a implementation of Entity Framework, so I would like to use the out of box version of identity with entity framework in my data access layer. How can I convert an UserStore<IdentityUser> to its base interface IUserStore<IUser> Once UserStore is an implementation of IUserStore, I can get the cast by this: UserStore<IdentityUser> as IUserStore<IdentityUser> But I want to avoid the

Entity Framework, Foreign key constraint may cause cycles or multiple cascade paths

末鹿安然 提交于 2021-02-11 04:31:31
问题 I use Entity Code first for my project. Basically I have 3 class Users , Branchs and UsersBranchs . Users contains UserID , Name ,... Branchs contains BranchID , Location , ... and UserID which is refer to creator of branch and UsersBranchs just have have two column BranchID and UserID which is define which user is in which branch the problem is I get this error: 'FK_dbo.UsersBranchs_dbo.Users_UsersID' on table 'UsersBranchs' may cause cycles or multiple cascade paths. Specify ON DELETE NO

How to get the the Schama.TableName in EF 6 database-first?

此生再无相见时 提交于 2021-02-10 22:39:58
问题 I'm using EF 6 database-first.. I use this method to get the name of the table: private static string GetTableName(ObjectContext context, Type entityType) { string entityTypeName = entityType.Name; EntityContainer container = context.MetadataWorkspace.GetEntityContainer(context.DefaultContainerName, DataSpace.CSpace); string tableName = (from meta in container.BaseEntitySets where meta.ElementType.Name == entityTypeName select meta.Name).First(); return tableName; } but it only returns the

EF Core - error with duplicated key while adding data with unique index

你。 提交于 2021-02-10 18:51:54
问题 I'm trying to add data to the database using EF Core, but I cannot overcome duplicate key error: Cannot insert duplicate key row in object 'dbo.Stocks' with unique index 'IX_Stocks_Name'. The duplicate key value is (stock1). I have entity Stock that has relation one-to-many with Transaction - one Stock can be related with many Transaction s. The problem doesn't occur only if there's no Stock with a given ID in the database - in that case I can add many Transaction s with the same StockID and

Nested Unit of Work in Entity Framework

自古美人都是妖i 提交于 2021-02-10 18:19:51
问题 I try to implement Unit of Work pattern in my application with use of nested units of work . I have the following interfaces: interface IDataService { IUnitOfWork NewUnitOfWork(); INestedUnitOfWork NewNestedUnitOfWork(IUnitOfWork parent); } interface IUnitOfWork : IDisposable { void Commit(); } interface INestedUnitOfWork : IUnitOfWork { IUnitOfWork Parent { get; } object GetParentObject(object obj); // get the same object in parent uow object GetNestedObject(object obj); // get the same

C# Linq Lambda Expression for Entity Framework Query Passing Custom Expression to Where Condition

流过昼夜 提交于 2021-02-10 14:51:10
问题 I have two tables called ShipmentType and Books. Entity class has been mapped for these tables. Created another class called BookShipment which contains two properties, classes of ShipmentType and Book. public class BookShipment { public ShipmentType Shipment { get; set; } public Books Book { get; set; } } I was trying to create a where expression as follows. Expression<Func<BookShipment, bool>> expr = x => (x.Shipment.ID == 1 && x.Book.ID == 1); var result = from c in styp join d in book on