标签: ef-fluent-api

如何使用方法语法连接linq to sql?

我在LINQ to SQL示例中看到了很多关于如何在查询语法中进行连接的示例,但我想知道如何使用方法语法来实现它?例如,我如何执行以下操作

var result = from sc in enumerableOfSomeClass
             join soc in enumerableOfSomeOtherClass
             on sc.Property1 equals soc.Property2
             select new { SomeClass = sc, SomeOtherClass = soc }
Run Code Online (Sandbox Code Playgroud)

用一个.Join()?谁能说明或提供另一个简单的例子吗?

linq-to-sql ef-fluent-api

187
推荐指数
3
解决办法
11万
查看次数

使用流畅的API设置唯一约束?

我正在尝试使用Code First构建一个EF实体,并EntityTypeConfiguration使用流畅的API.使用Unique Constraint创建主键很容易,但不是这样.我看到旧帖子建议为此执行本机SQL命令,但这似乎打败了目的.这可能与EF6有关吗?

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

181
推荐指数
5
解决办法
10万
查看次数

实体框架代码优先 - 流利Api与数据注释的优缺点

在使用Entity Framework代码优先创建数据库时,可以从代码中提取许多数据库模型.Fluent API和/或Attributes可用于微调模型.

与数据注释相比,Fluent Api有哪些优缺点?换句话说:即使在某些情况下可以使用这两种方法,在一种情况下,一种方法应该优先于另一种方法吗?

entity-framework ef-fluent-api

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

使用Entity Framework Fluent API的一对一可选关系

我们希望使用Entity Framework Code First使用一对一的可选关系.我们有两个实体.

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

    public int? LoyaltyUserDetailId { get; set; }
    public LoyaltyUserDetail LoyaltyUserDetail { get; set; }
}

public class LoyaltyUserDetail
{
    public int Id { get; set; }
    public double? AvailablePoints { get; set; }

    public int PIIUserId { get; set; }
    public PIIUser PIIUser { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

PIIUser可能有LoyaltyUserDetailLoyaltyUserDetail必须有PIIUser.我们尝试了这些流畅的方法技巧.

modelBuilder.Entity<PIIUser>()
            .HasOptional(t => t.LoyaltyUserDetail)
            .WithOptionalPrincipal(t => t.PIIUser)
            .WillCascadeOnDelete(true);
Run Code Online (Sandbox Code Playgroud)

这种方法没有 …

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

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

什么是Entity Framework流畅的api?

我一直听说实体框架流利的api,但我很难找到一个很好的参考.它是什么?

我们使用实体框架和提供的建模工具.这就是全部吗?或者是不同的东西?

同样,如果问题不是太宽泛,POCO是什么?我知道它代表Plain Old CLR Objects,但对于我已经使用EF设计模型工具的人来说,这对我意味着什么呢?如果这个问题太模糊,那么请忽略它.我刚刚在这里学习,你愿意提供的任何信息都很有帮助.

c# orm entity-framework entity-framework-4 ef-fluent-api

52
推荐指数
2
解决办法
3万
查看次数

Fluent API,Entity Framework Core 2.0中的多对多

我使用EF Core 2.0,Code first和Fluent API 搜索stackoverflow以获得生成多对多关系的正确解决方案.

一个简单的场景是:

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

如果我错了,请纠正我,但我老实说没有找到一个问题,其中包含如何使用所描述的工具进行详细说明.谁能解释一下这是怎么做到的?

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

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

如何通过Fluent API实体框架定义多对多关系?

以下是我的模型:

public class TMUrl
{
    //many other properties

    //only property with type Keyword
    public List<Keyword> Keywords{get;set;} 
}

public class Keyword
{
   //many other properties

   //only property with type TMUrl
   public List<TMUrl> Urls{get;set;}
}
Run Code Online (Sandbox Code Playgroud)

很明显,两个实体都有多对多的关系.我选择了流畅的api告诉实体框架关于这种关系,即

modelBuilder.Entity<TMUrl>
               .HasMany(s => s.Keywords)
               .WithMany(s => s.URLs).Map(s =>
                {
                    s.MapLeftKey("KeywordId");
                    s.MapRightKey("UrlId");
                    s.ToTable("KeywordUrlMapping");
                });
Run Code Online (Sandbox Code Playgroud)

但是当我这样做的时候

url.Keywords.Add(dbKey); //where url is object of TMUrl, 
                         //dbKey is an existing/new object of Keyword
db.SaveChanges();
Run Code Online (Sandbox Code Playgroud)

我得到例外

An error occurred while saving entities that do not expose foreign key 
properties for their relationships.... …
Run Code Online (Sandbox Code Playgroud)

c# many-to-many entity-framework ef-fluent-api

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

EF6.X中的EntityFramework代码第一个FluentAPI DefaultValue

如何使用EntityFramework Code First FluentAPI为bool属性设置默认值?

就像是:

Property(l => l.PropertyFlag).HasColumnType("bit").DefaultValue(1);
Run Code Online (Sandbox Code Playgroud)

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

31
推荐指数
2
解决办法
3万
查看次数

实体框架Linq查询:.Where chain vs &&

此问题涉及使用Linq和实体框架进行查询优化.

链接.Where子句与使用linq查询&&的单个.Where子句与实体框架之间有什么区别吗?

例如:假设我有以下代码:

var result = context.SomeEntity.Where(exp1).Where(exp2);
Run Code Online (Sandbox Code Playgroud)

要么

var result = context.SomeEntity.Where(exp1 && exp2);
Run Code Online (Sandbox Code Playgroud)

在评估产生相同结果的这些语句时,linq和实体框架是否以相同的方式评估它们?即,两者是否都有相同的执行计划,因此效率相同?

linq entity-framework ef-fluent-api

25
推荐指数
1
解决办法
3294
查看次数

使用HasColumnAnnotation可以实现多个索引?

它看起来像在Entity Framework 6.1中,它们添加了通过新HasColumnAnnotation方法创建表索引的功能.我创建了一些辅助扩展来加速这个过程:

public static class MappingExtensions
{
    public static StringPropertyConfiguration HasIndex(this StringPropertyConfiguration config, bool isUnique = false)
    {
        return config.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute() { IsUnique = isUnique }));
    }
    public static StringPropertyConfiguration HasIndex(this StringPropertyConfiguration config, string name, int order = 1, bool isUnique = false)
    {
        return config.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute(name, order) { IsUnique = isUnique }));
    }
    public static PrimitivePropertyConfiguration HasIndex(this PrimitivePropertyConfiguration config, bool isUnique = false)
    {
        return config.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute() { IsUnique = …
Run Code Online (Sandbox Code Playgroud)

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

17
推荐指数
1
解决办法
8122
查看次数