entity-framework

Two Foreign Keys in entity framework for same table

此生再无相见时 提交于 2020-01-25 00:07:14
问题 I have two entities: Municipality and RoadSemgents. Municipality is parent and RoadSegments is a child table. I need to have two foreign Keys in RoadSegments from the same table (Municipality). public class Municipality { [Key] public int ID { get; set; } public string Name { get; set; } } public class RoadSegments { [Key] public int ID { get; set; } //ForeignKeys public int CodeMunicipalityLeft_ID { get; set; } public int CodeMunicipalityRight_ID { get; set; } [ForeignKey(

Dependency Injection and multiple projects in solution

拈花ヽ惹草 提交于 2020-01-24 23:27:19
问题 I have been following this article concerning the use of the repository pattern and UnitOfWork with entity framework. I am also planning to use Ninject as my IOC container for an upcoming project. Given the sample code from the article, the NorthwindContext class in the NorthwindData project implements the IUnitOfWork interface which live inside of the NorthwindModel project. How can I utilize dependency injection to eliminate the dependency on NorthwindModel to NorthwindData? The repository

Autogenerate primary key (Guid) Entity Framework CTP5

百般思念 提交于 2020-01-24 21:33:27
问题 I have a following POCO class public class Account { [Key,DatabaseGenerated(DatabaseGenerationOption.Identity)] public string AccountId { set; get; } public string FirstName { set; get; } public string LastName { set; get; } public string Email { set; get; } } I get the following exception when the database gets created Identity column 'AccountId' must be of data type int, bigint, smallint, tinyint, or decimal or numeric with a scale of 0, and constrained to be nonnullable. 回答1: Shouldn't you

Entity Framework Code First: How to map SQL Server's computed columns into CF model?

你。 提交于 2020-01-24 21:29:10
问题 Is it possible to map computed columns into Code First model? I'd like to define computed columns with SQL Server and then map them as properties inside my CF model. Thanks in advance Best Regards 回答1: Use the DatabaseGenerated attribute with DatabaseGeneratedOption.Computed as the value [DatabaseGenerated(DatabaseGeneratedOption.Computed)] public string Foo { get; set; } 回答2: Not sure how you would do computed columns without using a view, and you cannot use views in EF Code First (you can

How are people unit testing with Entity Framework 6, should you bother?

喜你入骨 提交于 2020-01-24 21:05:08
问题 I am just starting out with Unit testings and TDD in general. I have dabbled before but now I am determined to add it to my workflow and write better software. I asked a question yesterday that kind of included this, but it seems to be a question on its own. I have sat down to start implementing a service class that I will use to abstract away the business logic from the controllers and map to specific models and data interactions using EF6. The issue is I have roadblocked myself already

Configuring Glimpse SQL Tab with N-Layer Architecture

六月ゝ 毕业季﹏ 提交于 2020-01-24 20:15:30
问题 I know it's often question regarding disabled SQL tab in Glimpse but I really can't make it work. I have N projects in Solution. Web and Database are seperated. I installed: Glimpse, Glimpse.EF6, Glimpse.MVC, Glimpse.Ado to Web project, but nothing seems work. I even tried to use GlimpseDbConnection in DbContext - still no result. public CemaDbContext() : base(cemaDbContextConnection, true) { } private static GlimpseDbConnection cemaDbContextConnection { get { var contextSection =

Multiplicity conflicts with the referential constraint Error

别来无恙 提交于 2020-01-24 20:06:30
问题 I get the following error when I run my application: One or more validation errors were detected during model generation: GuideMedApp.Models.NetworkPrescriber_Patients: : Multiplicity conflicts with the referential constraint in Role 'NetworkPrescriber_Patients_Source' in relationship 'NetworkPrescriber_Patients'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'. I'm trying to create a relationship between entities

Cannot add FK using EntityFramework

我们两清 提交于 2020-01-24 19:33:07
问题 I'm learning ASP.NET Core with Entity Framework and I'm trying to add an FK in my UserDetails table. These are the model: public class User { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public virtual UserDetails UserDetail { get; set; } } public class UserDetails { public string UserId { get; set; } public string Biography { get; set; } public string Country { get; set; } public Uri FacebookLink { get; set; } public Uri TwitterLink {

EF One-to-One Update fails

て烟熏妆下的殇ゞ 提交于 2020-01-24 16:15:49
问题 I have one-to-one relationship defined in EF6 which works for Inserts. Unfortunately when I try an update with a disconnected record, I receive an error. Here are details: .NET Source: namespace EF_ConsoleApp_Test { public class Program { public static void Main(string[] args) { int? accountId; int? customerId; using (var db = new MainContext()) { var account = new Account { AccountNumber = "1234", Customer = new Customer {FirstName = "John"} }; db.Accounts.Add(account); db.SaveChanges();

EF One-to-One Update fails

给你一囗甜甜゛ 提交于 2020-01-24 16:15:14
问题 I have one-to-one relationship defined in EF6 which works for Inserts. Unfortunately when I try an update with a disconnected record, I receive an error. Here are details: .NET Source: namespace EF_ConsoleApp_Test { public class Program { public static void Main(string[] args) { int? accountId; int? customerId; using (var db = new MainContext()) { var account = new Account { AccountNumber = "1234", Customer = new Customer {FirstName = "John"} }; db.Accounts.Add(account); db.SaveChanges();