ef-fluent-api

One or Zero to One Entity Framework Code First FluentApi

不想你离开。 提交于 2019-11-30 19:11:35
I need to create fluentapi one or zero to one reference and have navigation properties on both of entities. EntityTwo should contain simple proerty to store foreign key (EntityOneId) public class EntityOne { public int Id { get; set; } public EntityTwo EntityTwo { get; set; } } public class EntityTwo { public int Id { get; set; } public int EntityOneId { get; set; } public EntityOne EntityOne { get; set; } } public class MyDbContext : DbContext { protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); //some code trimmed modelBuilder.Entity

How to create spatial index using EF 6.1 fluent API

我的未来我决定 提交于 2019-11-30 03:38:49
问题 Well, the question is clear enough. Is it possible to create spatial indexes using Entity Framework 6.1 fluent API? 回答1: Short answer- No, it is not. I have seen this tangentially referenced throughout blogs and have found no concrete examples of implementation. It seems to be related to the fact that spatial indexes are filtered indexes, which are not supported in Entity Framework. As support for my answer I constructed a POC console app with the most recent version of Entity Framework (6.1)

Violation of primary key Entity Framework Code First

∥☆過路亽.° 提交于 2019-11-29 14:07:51
I have started with C# and I wanted to make my own DB. I have two models public class AModel { public Guid ID { get; private set; } public string Name { get; set; } public int Count { get; set; } public AModel() { this.ID = Guid.NewGuid(); } } public class BModel { public Guid ID { get; private set; } public string Name { get; set; } public AModel Model { get; set; } public BModel() { this.ID = Guid.NewGuid(); } } When I try to save BModel to DB, I get this error: Violation of PRIMARY KEY constraint 'PK_dbo.AModels'. Cannot insert duplicate key in object 'dbo.AModels'. The duplicate key value

How to set the one-to-one relationship with fluent api in this case? (EF6)

浪子不回头ぞ 提交于 2019-11-29 12:58:45
I have this two entities: public partial class Ficheros { public Guid Idfichero { get; set; } public long Iddocumento { get; set; } public byte[] Fichero { get; set; } public virtual Documentos IddocumentoNavigation { get; set; } } public partial class Documentos { public Documentos() { ElementosDocumentos = new HashSet<ElementosDocumentos>(); } public long Iddocumento { get; set; } public string Nombre { get; set; } public long? IdtipoDocumento { get; set; } public string Codigo { get; set; } public decimal? Espacio { get; set; } public string Unidades { get; set; } public long? Bytes { get;

Ef core fluent api set all column types of interface

余生长醉 提交于 2019-11-29 12:11:04
Unfortunately ef core does not support TPC-pattern, but we need this kind of behaviour. I´ve wrote an interface which is called IBase and each entity implements this interface: public interface IBase { Guid Id { get; set; } [Column(TypeName = "datetime2")] DateTime CreateDate { get; set; } [Required] [StringLength(255)] string CreateUser { get; set; } bool Deleted { get; set; } } I want to get rid of Annotations to use the Fluent API configuration instead. But I have about 20 different entities and 7 Base-Values and I don´t want to make the same configuration over and over again: modelBuilder

How to add an index on multiple columns with ASC/DESC sort using the Fluent API?

荒凉一梦 提交于 2019-11-28 12:33:37
I have a MVC ASP.NET application using Entity Framework 6 - Code First approach. Using the Fluent API, how can I add an index on multiple columns with ASC/DESC sort that is different for each column ? I've seen many examples using multiple columns but no way to set the sort order of the columns in the index. Table ----- Id Type DateFor DateCreated Value I want an index on the following columns: Type(ASC), DateFor(Desc), DateCreated(Desc). Short answer: Entity Framework 6 does not allow multiple indexes with different sorts. Long answer: It may not be possible to do it directly but it can be

Violation of primary key Entity Framework Code First

隐身守侯 提交于 2019-11-28 08:02:06
问题 I have started with C# and I wanted to make my own DB. I have two models public class AModel { public Guid ID { get; private set; } public string Name { get; set; } public int Count { get; set; } public AModel() { this.ID = Guid.NewGuid(); } } public class BModel { public Guid ID { get; private set; } public string Name { get; set; } public AModel Model { get; set; } public BModel() { this.ID = Guid.NewGuid(); } } When I try to save BModel to DB, I get this error: Violation of PRIMARY KEY

How to set the one-to-one relationship with fluent api in this case? (EF6)

删除回忆录丶 提交于 2019-11-28 06:52:12
问题 I have this two entities: public partial class Ficheros { public Guid Idfichero { get; set; } public long Iddocumento { get; set; } public byte[] Fichero { get; set; } public virtual Documentos IddocumentoNavigation { get; set; } } public partial class Documentos { public Documentos() { ElementosDocumentos = new HashSet<ElementosDocumentos>(); } public long Iddocumento { get; set; } public string Nombre { get; set; } public long? IdtipoDocumento { get; set; } public string Codigo { get; set;

How to create Lookup table and define relationships

风格不统一 提交于 2019-11-28 01:34:48
As you can see at below, there are a Lookup table for the enum values and I want to create a relationship between a table's enum values and LookupKey column of the Lookup table (instead of ID column of the Lookup table). Lookup table: ID | LookupType | LookupKey | LookupValue | 101 | Status | 0 | Passive | 106 | Gender | 1 | Male | 113 | Status | 1 | Active | 114 | Gender | 2 | Female | 118 | Status | 2 | Cancelled | Main Table: ID | Status | Gender | Name | ... 1 | 0 | 1 | John Smith | ... 2 | 1 | 2 | Christof Jahnsen | ... 3 | 2 | 1 | Alexi Tenesis | ... 4 | 0 | 2 | Jurgen Fechtner | ... 5 |

Entity Framework Core 2.0: How to configure abstract base class once

梦想的初衷 提交于 2019-11-28 01:07:41
I have a base model: public abstract class Status { public string updateUserName { get; set; } } Then a model which extends the base model defined above: public class Item : Status { public int Id { get; set; } public string Description { get; set; } } Then I have defined configuration classes for each: public class ItemConfiguration : IEntityTypeConfiguration<Item> { public void Configure(EntityTypeBuilder<Item> builder) { builder.ToTable("Item", "dbo").HasKey(c => c.Id); builder.Property(c => c.Description).IsRequired().HasMaxLength(100); } } public class StatusConfiguration :