标签: ef-fluent-api

如何在 EF Core 2.0 中使用 IEntityTypeConfiguration 设置foreignkey和index属性

我正在尝试使用EF Core 2.0 中新增的EntityConfiguration接口。

附录可能有助于查看 Message 类

public class Message 
{
    public int MessageId { get; set; }

    [Required]
    public string MessageForUserId { get; set; }
    public virtual ApplicationUser MessageForUser { get; set; }

    [Required]
    public string MessageFromUserId { get; set; }

    public virtual ApplicationUser MessageFromUser { get; set; }

    [Required]
    public string MessageContent { get; set; }

    public bool MessageRead { get; set; }

    [Required]
    public DateTime MessageCreatedOn { get; set; }

    public DateTime? MessageReadOn { get; …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework-core ef-fluent-api

3
推荐指数
1
解决办法
5778
查看次数

EF Core 一对多无限包含

我有两个具有一对多关系的表 Appointment 和 TaskAllocation。现在当我得到预约时

public IEnumerable<Appointment> GetAppointments(int employeeId, DateTime date)
    {
        return _context.Appointment.Where(a => a.EmployeeId == employeeId && 
            a.AppointmentDate == date)
           .Include(a=>a.Tasks).ToList();
    }
Run Code Online (Sandbox Code Playgroud)

它会导致包括一项包含多项任务的约会,以及一项包含多项任务的约会,等等。

entity-framework ef-fluent-api .net-core

3
推荐指数
1
解决办法
1514
查看次数

Entity Framework Core 零或一到零或一关系

鉴于这些类:

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;} 
}
Run Code Online (Sandbox Code Playgroud)

然后使用 Fluent API

 modelBuilder.Entity<A>()
                .HasOne(a => a.B)
                .WithOne(b => b.A)
                .HasForeignKey<A>(a => a.BId);
Run Code Online (Sandbox Code Playgroud)

创建对象并将它们添加到数据库时,相应表中的内容如下所示:

  • [A].BId 已设置
  • [B].AId = 空

当我使用 EF Core 检索数据时:

  • AB 已设置,A.BId 已设置
  • BA 已设置,但B.AId 为 null

我应该怎么做才能设置 B.AId?

c# entity-framework-core ef-fluent-api

3
推荐指数
1
解决办法
2894
查看次数

EF Core 添加迁移生成带有 ColumnName1 的额外列

当我生成迁移时,我有以下实体,它会创建两个名为 RestrictedCategoryId 和 RestrictedCategoryId1(FK) 的列。如何解决这个问题,只用 FK 生成一列?

注意:我需要每个实体中的 OrderId。

`C#

public class Order
{
    public Guid Id { get; set; }
    public DateTime OrderDate { get; set; }

    private List<Category> _categories;
    public List<Category> Categories => _categories;
}
public class Category
{
    public Guid Id { get; set; }
    public string Code { get; set; }
    public string Name { get; set; }

    public Guid OrderId { get; set; }
    public Order Order { get; set; }

    private List<RestrictionCategory> _restrictedCategories;
    public …
Run Code Online (Sandbox Code Playgroud)

sql-server ef-code-first entity-framework-core ef-fluent-api .net-core

3
推荐指数
1
解决办法
2560
查看次数

实体框架中基本实体的流畅配置

我有以下BaseEntity

public class BaseEntity
{
    public BaseEntity()
    {
        DateCreated = DateTime.UtcNow;
        DateModified = DateTime.UtcNow;
    }

    public DateTime DateCreated { get; set; }
    public DateTime DateModified { get; set; }

    [MaxLength(36)]
    public string CreateUserId { get; set; }

    [MaxLength(36)]
    public string ModifyUserId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我所有的其他实体都来自它.现在我想使用流畅的配置而不是DataAnnotations.我是否真的必须在每个DbModelBuilder配置中配置两个字符串属性的MaxLength ?

c# entity-framework ef-code-first ef-fluent-api

2
推荐指数
1
解决办法
1132
查看次数

EF 6 Codefirst-使用Fluent API为基类中定义的属性设置默认值

我有一个具有审计属性的基类,例如

public abstract class BaseModel
{
    [Column(Order = 1)] 
    public long Id { get; set; }
    public long CreatedBy { get; set; }
    public DateTime CreatedDate { get; set; }
    public long ModifiedBy { get; set; }
    public DateTime ModifiedDate { get; set; }
    public bool IsActive { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我所有的poco课程都来自于该课程。

我试图将默认值设置为IsActive属性。我不热衷于使用批注,因此如果我能使用流利的API来解决这个问题,我会一直徘徊。

我试过了,但是没有用。好像它创建了一个名为BaseModel的新表

modelBuilder.Entity<BaseModel>()
    .Property(p => p.IsActive)
    .HasColumnAnnotation("DefaultValue", true);
Run Code Online (Sandbox Code Playgroud)

有人可以在这里建议一种方法吗?

entity-framework entity-framework-6 ef-fluent-api

2
推荐指数
1
解决办法
2852
查看次数

实体框架6中的流畅Api不兼容实体框架核心

我使用Entity Framework 6实现了Fluent API.使用EnityFrameworkCore实现相同时遇到问题.

下面是使用EntityFramework 6的Fluent API的代码

 public class CustomerConfiguration : EntityTypeConfiguration<Customers>
    {
        public CustomerConfiguration()
        {
            ToTable("Customers");
            Property(c => c.FirstName).IsRequired().HasMaxLength(50);
            Property(c => c.LastName).IsRequired().HasMaxLength(50);
            Property(c => c.Gender).IsRequired().HasMaxLength(10);
            Property(c => c.Email).IsRequired().HasMaxLength(25);
            Property(c => c.Address).IsRequired().HasMaxLength(50);
            Property(c => c.City).IsRequired().HasMaxLength(25);
            Property(c => c.State).IsOptional().HasMaxLength(15);

        }
    }


  protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new CustomerConfiguration());
            modelBuilder.Configurations.Add(new OrderConfiguration());
            modelBuilder.Configurations.Add(new ProductConfiguration());

            modelBuilder.Entity<Orders>()
           .HasRequired(c => c.Customers)
           .WithMany(o => o.Orders)
           .HasForeignKey(f => f.CustomerId);

            modelBuilder.Entity<Orders>()
                .HasMany<Products>(s => s.Products)
                .WithMany(c => c.Orders)
                .Map(cs =>
                {
                    cs.MapLeftKey("OrderRefId");
                    cs.MapRightKey("ProductRefId");
                    cs.ToTable("OrderDetails");
                });


        }
Run Code Online (Sandbox Code Playgroud)

我在EntityFrameworkCore中遇到的问题是

  1. 它可以识别CustomerConfiguration()中的ToTable和Property关键字
  2. 它在OnModelCreating方法中识别出Configurations,HasRequired,MapLeftKey,MapRightKey,ToTable关键字 …

c# entity-framework entity-framework-core ef-fluent-api asp.net-core

2
推荐指数
1
解决办法
3673
查看次数

如何使用Entity Framework映射具有实体集合的派生类

我如何映射DogsCats收集,Toys但不是Rats因为它们不是宠物?

public abstract class Animal {
    public int Id { get; set; }
}

public class Cat : Animal {
    public virtual ICollection<Toy> Toys { get; set; }
}

public class Dog : Animal {
    public virtual ICollection<Toy> Toys { get; set; }
}

public class Rat : Animal {

}

public class Toy {
    public int Id { get; set; }

    public string Name { get; set; }

    public Animal Owner …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework ef-fluent-api

2
推荐指数
1
解决办法
134
查看次数

Ef core fluent api设置所有列类型的接口

不幸的是,ef core不支持TPC模式,但我们需要这种行为.我写了一个名为IBase的接口,每个实体都实现了这个接口:

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; }
}
Run Code Online (Sandbox Code Playgroud)

我想摆脱Annotations来改为使用Fluent API配置.但我有大约20个不同的实体和7个Base-Values,我不想一遍又一遍地做出相同的配置:

 modelBuilder.Entity<SomeEntity>()
            .Property(e => e.CreateDate)
            .HasColumnType("datetime2(2)")
            .IsRequired();
Run Code Online (Sandbox Code Playgroud)

任何想法如何为所有实施IBase的实体配置一次Base-Property?

c# entity-framework entity-framework-core ef-fluent-api .net-core

2
推荐指数
1
解决办法
2247
查看次数

仅使用FluentAPI使用外键定义实体框架关系

是否有任何方法使用FluentAPI(不应更改数据模型)仅使用外键(没有引用类型的虚拟属性)定义实体框架关系?

CardDataModel

public class CardDataModel
{
    public int CardId { get; set; }

}
Run Code Online (Sandbox Code Playgroud)

CheckItemDataModel

public class CheckItemDataModel
{
    public int CheckItemId { get; set; }
    public int CardId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

.net c# entity-framework entity-framework-core ef-fluent-api

2
推荐指数
1
解决办法
556
查看次数