ef-fluent-api

EF6 CodeFirst One to Many MySQL a foreign key constraint fails

岁酱吖の 提交于 2019-12-08 09:06:25
问题 I'm doing a Mapping from existing Database to CodeFirst and I'm having a problem with One To Many Relationships. System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.Entity. Core.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> MySql.Data.MySqlClient.MySqlException: Cannot add or update a child row: a foreign key constraint fails I have a

Entity Framework 6.1.3 Foreign Key on non primary key field

穿精又带淫゛_ 提交于 2019-12-08 05:58:09
问题 I have the following 2 classes: public class UserMembership: Entity { public Guid Id { get; set; } public Guid UserGroupId { get; set; } public string LoginId { get; set; } public Guid SiteId { get; set; } public virtual Site Site { get; set; } public virtual SecurityUser SecurityUser { get; set; } } and public class SecurityUser: Entity { ICollection<UserMembership> _userMemberships = new Collection<UserMembership>(); public Guid Id { get; set; } public string LoginId { get; set; } public

EF Fluent API Many To Many with different ID Field Names

拜拜、爱过 提交于 2019-12-07 06:24:20
问题 In this question: Ef Many To Many, an answer came up on how to manually specify a linking table. But I have a slightly unique situation (which I'm sure isn't really unique). My two tables each have an Id field. E.G.: [dbo].[Account].[Id] and [dbo].[Person].[Id] . Each of these tables in my Code-First has the following OnModelCreating: modelBuilder.Entity<Account>.HasKey(x => x.Id); modelBuilder.Entity<Person>.HasKey(x => x.Id); But my [dbo].[AccountsToPersons]... table has fields E.G.:

TPC Inheritance Error

浪尽此生 提交于 2019-12-07 03:39:56
问题 I've got an strange problem with TPC inheritance using C# Entity Framework Codefirst and Fluent Api. I have 3 Classes named Person , Invoice and PeriodicInvoice as you can see below. Here is a summary of my code: Invoice class and its configuration class: public class Invoice : InvoiceBase { public Person User { get; set; } } public class InvoiceConfig : EntityTypeConfiguration<Invoice> { public InvoiceConfig() { this.Map(m => { m.MapInheritedProperties(); m.ToTable("Invoices"); }); } }

Entity-framework-7 Organizing Fluent API configurations into a separate class

坚强是说给别人听的谎言 提交于 2019-12-07 03:24:19
问题 I'm familiar how to organize the fluent API configurations into a separate class on EF6, but how is this achieved with EF7? Here is an example how to do it with EF6: ModelConfigurations.cs public class ModelConfigurations : EntityTypeConfiguration<Blog> { ToTable("tbl_Blog"); HasKey(c => c.Id); // etc.. } and to call it from OnModelCreating() protected override void OnModelCreating(DbModelbuilder modelBuilder) { modelBuilder.Configurations.Add(new ModelConfigurations()); // etc... } On EF7 I

Define SQL table primary key as case sensitive using Entity Framework Code First

夙愿已清 提交于 2019-12-06 13:31:08
Is it possible to define a SQL case sensitive primary key in entity framework code first? I know that by default SQL is case insensitive when it comes to strings, just to be clear I don't want to change the entire DB definition to case sensitive, just one table column (primary key). I'm getting data from an external API which sends the data with case sensitive primary keys ('a' and 'A' are different records), I know I can modify them and save them differently in my DB, but this will also require me to be consistent about it everywhere in my code. That's defiantly possible, but I would rather

Creating a UNIQUE Filtered Index for NULL values on Entity Framework

一世执手 提交于 2019-12-06 08:02:54
问题 I'm trying to create a table that has a UNIQUE Filtered Index for NULL values (eg. Allow Null values to be duplicates) using Entity Framework. I am using Fluent API and have this entity property: modelBuilder.Entity<Client>().Property(c => c.Barcode) .HasMaxLength(20) .IsRequired() .HasColumnAnnotation( IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_ClientBarcode") { IsUnique = true })); I found that SQL Server 2008 allows this for unique columns with filtered

Entity Framework Core zero-or-one to zero-or-one relation

ぃ、小莉子 提交于 2019-12-05 23:16:43
Given these classes: public class A { public int Id {get; set;} public int? BId {get; set;} public B B {get; set;} } public class B { public int Id {get; set;} public int? AId {get; set;} public A A {get; set;} } Then with Fluent API modelBuilder.Entity<A>() .HasOne(a => a.B) .WithOne(b => b.A) .HasForeignKey<A>(a => a.BId); When creating objects and add them to database, things look like following in the corresponding tables: [A].BId is set [B].AId = null When I retrieve data using EF Core: A.B is set, A.BId is set B.A is set, but B.AId is null . What should I do to have B.AId set as well?

Set a constraint for minimum int value

百般思念 提交于 2019-12-05 13:46:10
I am using the Repository pattern. I have a entity called Product and I want to set the minimum value for price to avoid zero prices. Is it possible to create it in a EntitytypeConfiguration class? My product configuration class public class ProductConfiguration : EntityTypeConfiguration<Product> { public PlanProductConfiguration(string schema = "dbo") { ToTable(schema + ".Product"); HasKey(x => new { x.IdProduct }); Property(x => x.Price).HasColumnName("flt_price") .IsRequired() .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); } } For server side validation, implement

EF Fluent API Many To Many with different ID Field Names

一曲冷凌霜 提交于 2019-12-05 08:35:28
In this question: Ef Many To Many , an answer came up on how to manually specify a linking table. But I have a slightly unique situation (which I'm sure isn't really unique). My two tables each have an Id field. E.G.: [dbo].[Account].[Id] and [dbo].[Person].[Id] . Each of these tables in my Code-First has the following OnModelCreating: modelBuilder.Entity<Account>.HasKey(x => x.Id); modelBuilder.Entity<Person>.HasKey(x => x.Id); But my [dbo].[AccountsToPersons]... table has fields E.G.: [AccountId] and [PersonId] The AccountsToPersons table is not represented by a class in code. I obviously