标签: ef-fluent-api

如何使用EF 6.1 fluent API创建空间索引

嗯,这个问题很清楚.是否可以使用Entity Framework 6.1流畅的API 创建空间索引

entity-framework spatial spatial-index entity-framework-6.1 ef-fluent-api

8
推荐指数
2
解决办法
1480
查看次数

一个或零到一个实体框架代码First FluentApi

  1. 我需要创建一个或0到一个引用的fluentapi,并在两个实体上都有导航属性.
  2. EntityTwo应该包含存储外键的简单proerty(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<EntityOne>()
                .HasOptional(entity => entity.EntityTwo)
                .WithRequired();
    
            modelBuilder.Entity<EntityTwo>()
                .HasRequired(entity => entity.EntityOne)
                .WithMany()
                .HasForeignKey(entity => entity.EntityOneId)
                .WillCascadeOnDelete(false);
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

更复杂的场景: …

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

8
推荐指数
1
解决办法
1万
查看次数

与Entity Framework Fluent API的一对一关系

我在我的一个实体上进行反向导航时遇到问题.

我有以下两个对象:

public class Candidate
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public long CandidateId { get; set; } 
    ....

    // Reverse navigation
    public virtual CandidateData Data { get; set; }
    ...

    // Foreign keys
    ....
}

public class CandidateData
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public long CandidateDataId { get; set; }

    [Required]
    public long CandidateId { get; set; }

    // Foreign keys
    [ForeignKey("CandidateId")]
    public virtual Candidate Candidate { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

现在我在CandidateData对象上的外键导航工作正常.我无法获得候选对象的反向导航(如果可能的话).

这是我的OnModelCreating函数:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

    modelBuilder.Entity<Candidate>()
        .HasOptional(obj => obj.Data) …
Run Code Online (Sandbox Code Playgroud)

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

7
推荐指数
1
解决办法
1万
查看次数

为最小 int 值设置约束

我正在使用存储库模式。我有一个名为 Product 的实体,我想设置价格的最小值以避免零价格。是否可以在 EntitytypeConfiguration 类中创建它?

我的产品配置类

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

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

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

违反主要密钥实体框架代码

我已经开始使用C#了,我想创建自己的数据库.

我有两个型号

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

当我尝试将BModel保存到DB时,我收到此错误:

违反PRIMARY KEY约束'PK_dbo.AModels'.无法在对象'dbo.AModels'中插入重复键.重复键值为(48ee1711-8da4-46c1-a714-19e985211fed).\ r \n语句已终止.

我以为它会被解决

modelBuilder.Entity<BModel>().HasRequired(t => t.Model).WithMany();
Run Code Online (Sandbox Code Playgroud)

但看起来我完全迷失了.这个简单的例子可以帮助我吗?

c# entity-framework ef-fluent-api

7
推荐指数
1
解决办法
1万
查看次数

EF核心中缺少System.Data.Entity.ModelConfiguration

尝试在OnModelCreating for Entity框架核心上动态加载所有配置.如果缺少ModelConfiguration,那么另一种方法是什么.

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

7
推荐指数
2
解决办法
1557
查看次数

如何在EF Core中配置固定长度列?

在之前的EF中,我可以这样做:

  entityTypeBuilder
    .Property(b => b.Foo)
    .IsRequired()
    .HasMaxLength(10)
    .IsFixedLength();
Run Code Online (Sandbox Code Playgroud)

这将产生类似的迁移

Foo = c.String(nullable: false, maxLength: 10, fixedLength: true)
Run Code Online (Sandbox Code Playgroud)

但是在EF Core中没有IsFixedLength().

还有其他方法吗?

entity-framework ef-code-first ef-migrations entity-framework-core ef-fluent-api

7
推荐指数
2
解决办法
3214
查看次数

可以在Entity Framework中设置列排序

是否有任何可能的配置来设置实体框架代码中的数据库列排序第一种方法..?

我的所有实体集都应该有一些用于保存recordinfo的公共字段

public DateTime CreatedAt { get; set; }
public int CreatedBy { get; set; }
public DateTime ModifiedAt { get; set; }
public int ModifiedBy { get; set; }
public bool IsDeleted { get; set; }
Run Code Online (Sandbox Code Playgroud)

我希望将这些字段保留在表的末尾.是否有任何可能的EF配置可用于配置此配置,而不是将此字段保留在我的模型类的末尾.

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

7
推荐指数
1
解决办法
1万
查看次数

EF是否可以使用阴影属性创建Mutli-column索引?

我正在尝试使用shadow属性创建多列唯一索引。我知道我可以通过添加属性来解决此问题,但是我想看看是否有可能做到这一点,而不需要保持模型清洁。

要创建多列索引,您可以在Fluent API中使用以下选项:

modelBuilder.Entity<AlbumTrack>().HasIndex(t => new { t.TrackNumber, t.AlbumId).IsUnique();
Run Code Online (Sandbox Code Playgroud)

但是我不想用额外的AlbumId属性使我的模型混乱,因此不想使用shadow属性,对于单个列,这是可行的:

modelBuilder.Entity<AlbumTrack>().HasIndex(t => EF.Property<int>(t,"AlbumId")).IsUnique();
Run Code Online (Sandbox Code Playgroud)

但是对于使用阴影属性的多列索引,如下所示

modelBuilder.Entity<AlbumTrack>()
    .HasIndex(t => new { t.TrackNumber, EF.Property<int>(t,"AlbumId")})
    .IsUnique();
Run Code Online (Sandbox Code Playgroud)

我在vscode中收到以下错误

无效的匿名类型成员声明符。必须使用成员分配,简单名称或成员访问来声明匿名类型成员。

任何人都有一个想法,如何使用阴影属性来做到这一点,还是不可能?

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

7
推荐指数
1
解决办法
567
查看次数

EF Core 中的一对一关系(无法确定一对一关系的子/依赖方)

我一直收到以下错误消息,但我似乎无法理解我收到它的原因。有趣的是,在添加迁移时我没有收到任何错误,但是每当我想使用上下文时,我都会得到它。

无法确定“Block.JobBlock”和“JobBlock.Block”之间的一对一关系的子/依赖方。要识别关系的子/依赖方,请配置外键属性。如果这些导航不应该是同一关系的一部分,请在不指定反向的情况下配置它们。

AJob可以有多个JobBlocks(一对多);singleBlock只能有一个JobBlock(一对一)。所以基本上, aJobBlock是一个参考表/实体,用于引用Job及其Blocks. 值得一提的是,JobBlock实体中的主键由两个键组成,因此成为复合主键。

有人可能会争辩说,一个Block实体应该已经包含一个IdJob属性,并且该JobBlock实体可以完全被驳回,但有一些理由说明为什么不应该这样做,所以让我们保持原样:)

楷模:

public class Job : IEntity
{
    public Job()
    {
        JobBlocks = new HashSet<JobBlock>();
    }

    public Guid Id { get; set; } = Guid.NewGuid();
    public ICollection<JobBlock> JobBlocks { get; set; }
}

public class Block : IEntity
{
    public Guid Id { get; set; } = Guid.NewGuid();
    public JobBlock JobBlock { get; …
Run Code Online (Sandbox Code Playgroud)

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

7
推荐指数
1
解决办法
7750
查看次数