ef-fluent-api

Ef core fluent api set all column types of interface

痴心易碎 提交于 2019-12-18 07:11:50
问题 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

Fluent API, many-to-many in Entity Framework Core

自作多情 提交于 2019-12-17 07:14:48
问题 I've searched stackoverflow for a proper solution on generating a many-to-many relationship, using EF Core, Code first and Fluent API. A simple scenario would be: public class Person { public Person() { Clubs = new HashSet<Club>(); } public int PersonId { get; set; } public virtual ICollection<Club> Clubs { get; set; } } public class Club { public Club() { Persons = new HashSet<Person>(); } public int ClubId { get; set; } public virtual ICollection<Person> Persons { get; set; } } Please

How to declare one-to-many using Fluent API without changing the model?

ぃ、小莉子 提交于 2019-12-13 06:12:10
问题 I have two classes as follows. class Donkey { public Guid Id { get; set; } } class Monkey { public Guid Id { get; set; } public Donkey Donkey { get; set; } } Now I want to configure the schema so that the relation is set in the database. Using Fluent API, I'll go something like this. protected override void OnModelCreating(DbModelBuilder model) { base.OnModelCreating(model); model.HasDefaultSchema("dbo"); ... model.Entity<Monkey> .HasRequired(_ => _.Donkey) .WithMany(_ => _.Monkeys) .Map(_ =>

Remove certain properties from object upon query EF Core 2.1

旧时模样 提交于 2019-12-13 03:39:52
问题 I'm trying to null or remove the ID entirely of all the queried IsoDataTables before returning them to frontend. The idea is that it should behave (in this case) as a template and I don't want it returning the id's back to me, nor do I want them to be removed in the frontend. var applicationType = await _context.ApplicationType .Include(m => m.IsoTableData) .AsNoTracking() .FirstOrDefaultAsync(m => m.Id == id); if (applicationType == null) { return NotFound(); } if (applicationType

One-to-one and one-to-many on the same entity in Entity Framework

倾然丶 夕夏残阳落幕 提交于 2019-12-13 03:35:53
问题 I tried different ways to map an entity as one-to-one and one-to-many at the same time, but I had no luck. I provided my models here: public class Office { public virtual Person Manager {get; set;} public virtual List<Person> People {get; set;} } public class Person { public virtual Office Office{get;set;} } Could anybody guide me to write mapping via fluent api? 回答1: It's not necessary to define two different mapping for specific entities. All I need was to map a one-to-many relation via

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

丶灬走出姿态 提交于 2019-12-12 09:57:45
问题 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

Set a constraint for minimum int value

人走茶凉 提交于 2019-12-12 09:44:56
问题 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()

How do I configure a navigation property to the same table in Entity Framework?

青春壹個敷衍的年華 提交于 2019-12-12 03:17:04
问题 How do I configure Entity Framework using fluent configuration to behave the same way that I would do this with attributes: public class Product { public int? ParentId { get; set; } [ForeignKey("ParentId")] public virtual Product Parent { get; set; } } 回答1: Supposing that you want to create a self referencing entity, I assume that you have a Product class like this: public class Product { public int Id { get; set; } public int? ParentId { get; set; } public virtual Product Parent { get; set;

How to declare a one-to-many in Fluent API so that it's not required?

核能气质少年 提交于 2019-12-12 02:59:00
问题 Suppose we have the two classes below. class Donkey { public Guid Id { get; set; } } class Monkey { public Guid Id { get; set; } public Donkey Donkey { get; set; } } I can configure the relation as follows. protected override void OnModelCreating(DbModelBuilder model) { base.OnModelCreating(model); model.HasDefaultSchema("dbo"); ... model.Entity<Monkey> .HasRequired(_ => _.Donkey) .WithMany() .Map(_ => _.MapKey("DonkeyId")); } I'd like to make it not-required, i.e. so that Donkey property can

EntityFramework - One to many relationship

烈酒焚心 提交于 2019-12-12 01:39:48
问题 I have a one-to-many relationship between MapHeader and MapDetail tables therefore there can be multiple mapdetails for a single mapheader. In the database, table MapDetail has foreign key MapHeaderID which maps to the pk(MapHeaderId) in MapHeader table. I have defined it in EntityFramework Code-first as follows: public class MapHeader { public int MapHeaderID { get; set; } .... public virtual ICollection<MapDetail> mapDetails { get; set; } } public class MapDetail { public int MapDetailID {