我在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()?谁能说明或提供另一个简单的例子吗?
我正在尝试使用Code First构建一个EF实体,并EntityTypeConfiguration使用流畅的API.使用Unique Constraint创建主键很容易,但不是这样.我看到旧帖子建议为此执行本机SQL命令,但这似乎打败了目的.这可能与EF6有关吗?
在使用Entity Framework代码优先创建数据库时,可以从代码中提取许多数据库模型.Fluent API和/或Attributes可用于微调模型.
与数据注释相比,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可能有LoyaltyUserDetail但LoyaltyUserDetail必须有PIIUser.我们尝试了这些流畅的方法技巧.
modelBuilder.Entity<PIIUser>()
.HasOptional(t => t.LoyaltyUserDetail)
.WithOptionalPrincipal(t => t.PIIUser)
.WillCascadeOnDelete(true);
Run Code Online (Sandbox Code Playgroud)
这种方法没有 …
我一直听说实体框架流利的api,但我很难找到一个很好的参考.它是什么?
我们使用实体框架和提供的建模工具.这就是全部吗?或者是不同的东西?
同样,如果问题不是太宽泛,POCO是什么?我知道它代表Plain Old CLR Objects,但对于我已经使用EF设计模型工具的人来说,这对我意味着什么呢?如果这个问题太模糊,那么请忽略它.我刚刚在这里学习,你愿意提供的任何信息都很有帮助.
我使用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)
如果我错了,请纠正我,但我老实说没有找到一个问题,其中包含如何使用所描述的工具进行详细说明.谁能解释一下这是怎么做到的?
以下是我的模型:
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) 如何使用EntityFramework Code First FluentAPI为bool属性设置默认值?
就像是:
Property(l => l.PropertyFlag).HasColumnType("bit").DefaultValue(1);
Run Code Online (Sandbox Code Playgroud) 此问题涉及使用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和实体框架是否以相同的方式评估它们?即,两者是否都有相同的执行计划,因此效率相同?
它看起来像在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) ef-fluent-api ×10
c# ×7
code-first ×1
linq ×1
linq-to-sql ×1
many-to-many ×1
one-to-one ×1
orm ×1