GVKun编程网logo

实体框架6.1-使用INCLUDE语句创建索引(createindex语句创建索引关键字)

7

如果您想了解实体框架6.1-使用INCLUDE语句创建索引的相关知识,那么本文是一篇不可错过的文章,我们将对createindex语句创建索引关键字进行全面详尽的解释,并且为您提供关于asp.net–

如果您想了解实体框架6.1-使用INCLUDE语句创建索引的相关知识,那么本文是一篇不可错过的文章,我们将对createindex语句创建索引关键字进行全面详尽的解释,并且为您提供关于asp.net – 实体框架 – 使用.Include()和.Select()的“eager loading” – 如何使用LEFT JOIN而不是INNER JOIN?、c – 在#include语句中使用垃圾字符时无编译器错误、c# – 实体框架6 – 仅使用引用不同表的主键的表、c# – 实体框架6中的基类?的有价值的信息。

本文目录一览:

实体框架6.1-使用INCLUDE语句创建索引(createindex语句创建索引关键字)

实体框架6.1-使用INCLUDE语句创建索引(createindex语句创建索引关键字)

因此,既然现在在最新的Entity Framework 6.1
Beta版本中都可以使用索引,那么是否甚至有可能以代码优先的方式创建与该SQL语句相等的索引?

CREATE NONCLUSTERED INDEX [Sample1]ON [dbo].[Logs] ([SampleId],[Date])INCLUDE ([Value])

答案1

小编典典

严格来说,在代码优先迁移中这一直是可能的,因为您可以在迁移中运行sql:

   public partial class AddIndexes : DbMigration    {        private const string IndexName = "IX_LogSamples";        public override void Up()        {            Sql(String.Format(@"CREATE NONCLUSTERED INDEX [{0}]                               ON [dbo].[Logs] ([SampleId],[Date])                               INCLUDE ([Value])", IndexName));        }        public override void Down()        {            DropIndex("dbo.Logs", IndexName);        }    }

但是我意识到您可能实际上是在问是否可以使用6.1中引入的IndexAttribute创建索引,但是要使用Include列-答案是“否”

asp.net – 实体框架 – 使用.Include()和.Select()的“eager loading” – 如何使用LEFT JOIN而不是INNER JOIN?

asp.net – 实体框架 – 使用.Include()和.Select()的“eager loading” – 如何使用LEFT JOIN而不是INNER JOIN?

这是我正在使用.NET 4.0在Entity Framework 5.0.0 RC(代码优先)中进行的查询

我是实体框架的新手,所以我仍然关注如何构建查询,特别是在选择“子”相关数据时.

我正在使用“急切加载”,因此我立即获得所有相关数据.但我遇到的问题是并非所有的Drop都被检索到了.

var loads = context.Loads
            .Include(
                p => p.Device
            )
            .Include(
                p => p.Drops.Select(
                    a => a.Customer
                )
            ).Include(
                q => q.Drops.Select(
                   b => b.Items.Select(
                       c => c.Product
                   )
                )
            ).Where(
                u => u.Id.Equals(id)
            );

问题是,在生成的SQL查询中,客户正在内联到Drops,因此排除了没有Customer的Drops.

那么如何让它在这两个实体之间进行LEFT JOIN?

.Include似乎做左连接 – 所以为什么不.选择?

有没有其他方法.选择我可以使用哪种方法进行LEFT JOIN?

UPDATE

在与Amiram聊天后,我意识到我已经错误地设置了Drop模型.我需要将CustomerID列设置为可选:

public class Drop
{
    public int Id { get; set; }
    public int? CustomerId { get; set; }
    public int LoadId { get; set; }
    public DateTime Date { get; set; }

    public virtual Customer Customer { get; set; }
    public virtual ICollection<DropItem> Items { get; set; }
}

我应该立即想到这一点,但说实话,我被推迟了.Include()始终执行LEFT JOIN这一事实,无论模型关系中的基数如何.我在想.Select()必须有一些类似的行为,但不是它只是遵守模型的配置:)

解决方法

Drop是内部与客户联系的,因为Drop.CustomerID的类型为int而不是nullable int(在聊天中查找).

c – 在#include语句中使用垃圾字符时无编译器错误

c – 在#include语句中使用垃圾字符时无编译器错误

#include <iostream> gfhgfhgfusing namespace std;int main() {    return 0;}

为什么这段代码段编译?根据The gcc reference on Include Syntax:

It is an error if there is anything (other than comments) on the line after the file name.

这正是代码中正在做的.

解决方法

使用gcc和clang中的-anticantic-errors标志将其转为错误 see it live:
error: extra tokens at end of #include directive#include <iostream> gfhgfhgf                    ^

这表明它是一个扩展.

如果我们看看Interfacing C and TAL In The Tandem Environment他们有一些这样的代码:

#include <stdlibh> nolist                   ^^^^^^

所以gcc和clang都可以在include指令之后支持额外的字符,以支持某些平台上需要的扩展.使用-pedantic flags使gcc和clang对违反标准的扩展产生警告,如上所述,您可以使用-pendatic错误将其转换为错误(强调我的):

to obtain all the diagnostics required by the standard,you should
also specify -pedantic (or -pedantic-errors if you want them to be
errors rather than warnings).

我们可以在HP’sC/C++ Programmers guide for NonStop Systrms中找到诺尔扩展的参考,它说:

06002

注意,draft C++ standard在第16.2节[cpp.include]中定义了这种形式的包含的语法,如下所示:

# include < h-char-sequence> new-line

c# – 实体框架6 – 仅使用引用不同表的主键的表

c# – 实体框架6 – 仅使用引用不同表的主键的表

当我们离开 Linq2sql时,我们正在学习Entity Framework 6.1(从NuGet).我们有一小撮桌子,可以连接两个独立的桌子,如下所示.

EF6数据库第一代

DB图:

模式概述:

当在Visual工作室,空白类库中,做一个Database First EF6 EDMX文件时,该图只生成TableA和TableC – 不会生成TableB.

视觉工作室视图:

您可以看到仅创建了TableA和TableC.技术上应该已经创建了TableB,因为您希望能够管理这些引用.

A和C之间的关联如图所示:

我觉得我错过了一个选择,或者误解了实体框架的一个关键概念.任何想法如何使用T4生成的缺少的TableB? EDMX文件显示它,但由于某些原因,它不会生成到.CS文件中,其中两个属性指示关系.

我们需要的主要原因是扩展了EF6 T4模板,以添加一些工厂模式来匹配我们现有的模型.因为它没有为TableB生成一个类,所以我们不会得到我们正在寻找的自动生成代码.

想法/建议?谢谢.

解决方法

EF不会生成弱实体或连接表,您需要通过流畅的api手动配置关系或使用数据注释

如微软网站所述:“关系公约”:

Note:If you have multiple relationships between the same types (for
example,suppose you define the Person and Book classes,where the
Person class contains the ReviewedBooks and AuthoredBooks navigation
properties and the Book class contains the Author and Reviewer
navigation properties) you need to manually configure the
relationships by using Data Annotations or the fluent API. For more
information,see Data Annotations – Relationships and Fluent API –
Relationships.

有关更多信息,请参阅此link

更新

如果EDMX(但是维护成本),解决方法将适用于以下情况:

>从数据库中的连接表中删除外键
>从数据库更新EDMX
>在连接表中重新创建外键

只要您不再从数据库更新您的模型,此解决方法将会起作用.

推荐的解决方案,保留EDMX生成的所有内容,并使用用户“@TravisWhidden”报告“有帮助”的以下链接,详细了解如何使用crud操作

> stackoverflow.com/questions/425316
> https://www.youtube.com/watch?v=uMQwORSTGX4(视频)

c# – 实体框架6中的基类?

c# – 实体框架6中的基类?

我的程序中有很多通用方法,它们将一些生成的实体作为参数.所以,方法如:

public void DoHerpDerp<EntityType>()

虽然这很好并且完成了工作,但我的方法的用户仍然可以将他们想要的任何东西作为通用参数传递(并使应用程序崩溃).我想严格限制它们为实体生成的对象(我正在使用Database First方法).我想写的是:

public void DoHerpDerp<EntityType>() where EntityType : BaseEntity

是否有像BaseEntity这样的类,如果不是一个,我该如何解决这个问题呢?不,我不会写200个实现接口的部分类.

解决方法

您可以通过调整T4模板来更改实体的生成.

这是用于生成类声明的T4模板(例如Model.tt)的相关部分,例如,“部分类MyEntity”:

public string EntityClassopening(EntityType entity)
{
    return string.Format(
        CultureInfo.InvariantCulture,"{0} {1}partial class {2}{3}",Accessibility.ForType(entity),_code.SpaceAfter(_code.AbstractOption(entity)),_code.Escape(entity),_code.StringBefore(" : ",_typeMapper.GetTypeName(entity.BaseType)));
}

public string EntityClassopening(EntityType entity)
{
    return string.Format(
        CultureInfo.InvariantCulture,"{0} {1}partial class {2}{3}{4}",_typeMapper.GetTypeName(entity.BaseType)),string.IsNullOrEmpty(_code.StringBefore(" : ",_typeMapper.GetTypeName(entity.BaseType))) ? _code.StringBefore(" : ","BaseClass") : "");
}

在这个例子中,每个没有超类的类都被生成为BaseClass的子类,您可以根据需要实现它.

今天的关于实体框架6.1-使用INCLUDE语句创建索引createindex语句创建索引关键字的分享已经结束,谢谢您的关注,如果想了解更多关于asp.net – 实体框架 – 使用.Include()和.Select()的“eager loading” – 如何使用LEFT JOIN而不是INNER JOIN?、c – 在#include语句中使用垃圾字符时无编译器错误、c# – 实体框架6 – 仅使用引用不同表的主键的表、c# – 实体框架6中的基类?的相关知识,请在本站进行查询。

本文标签: