如何在EF中制作视图。 [英] how to make a view in EF.

查看:129
本文介绍了如何在EF中制作视图。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好Freind

我有一个问题。我在google搜索了但是我找不到具体的回复。

i我是Ef上的新朋友。

i想要做下面的图片。

http://upload7.ir/viewer.php?file=13346259503430023518.jpg

确实我想要一个像自然加入或在sql中制作视图的东西。(类似于视图)

i希望通过Ef(Model_First)和(Code_first)方式来实现理解它是怎么做的。

i已经阅读并知道配置关系(一对一|一对多|多对多)
两个表之间的
但我做不知道怎么用EF中的两个表来做到这一点。

i需要一个样本来解释那个并在那张桌子上做Crud操作。



非常感谢。

hello Freind
i have a question . i searched in google for that but i Could not find a specific reply.
i am new on Ef.
i want to do a thing like following picture.
http://upload7.ir/viewer.php?file=13346259503430023518.jpg
indeed i want a thing like Natural Join Or making A view in sql.(similar to view)
i want to do it by Ef in (Model_First)and(Code_first) ways to understand How do it.
i have read and know Configure Relationships (One-to-One | one-to-Many | many to many)
between two Tables but i Do not Know how to do that with more two TABLES in EF.
i need a sample that Explain Detail of that and doing Crud operation on that table.

thanks a lot.

推荐答案

看起来你正试图让EF表现得像SQL一样,这真的不是它的目的。许多将SQL作为数据提供程序服务运行的基本概念(如视图和存储过程)与EF的工作方式关系不大。 EF可以使用这些东西,你的程序可以模仿这些东西,但它不会特别复制这些东西。



虚拟关键字很大程度上是你声明关系的方式。 br />


自然联接很容易。一对多看起来像:



It looks like you're trying to make EF behave like SQL, which really isn't its purpose. Many of the concepts that are fundamental to running SQL as a data provider service, such as views and stored procedures, do not have much bearing on how EF works. EF can use those things, and your program can emulate these things, but it will not specifically duplicate these things.

The virtual keyword is largely how you declare relationships.

Natural joins are easy. One to many looks like:

public class Foo
{
    public int Id { get; set; }
    public virtual ICollection<Bar> Bars { get; set; }
}

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

    [ForeignKey("Foo")]
    public int fooId{ get; set; }

    public virtual Foo Foo { get; set; }
}

public class MyContext : DbContext
{
    public DbSet<Foo> Foos { get; set; }
    public DbSet<Bar> Bars { get; set; }
}



编辑:



我忘记提及,你可以援引财产 - 基于联接作为标准属性,例如:




I forgot to mention, you can invoke the property-based joins as a standard property, such as:

Foo myFoo = context.Foos.First(//find the foo I'm looking for);
List<Bar> myBar = myFoo.Bars;







M:M:






M:M :

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

    public virtual ICollection<FooBar> FooBar { get; set; }
}

public class Bar
{
    public int Id { get; set; }
    public virtual ICollection<FooBar> FooBar { get; set; }
}

public class FooBar
{
    [ForeignKey("Foo")]
    public int fooId { get; set; }
    [ForeignKey("Bar")]
    public int barId { get; set; }
    
    public virtual Foo Foo { get; set; }
    public virtual Bar Bar { get; set; }
}

public class MyContext : DbContext
{
    public DbSet<Foo> Foos { get; set; }
    public DbSet<Bar> Bars { get; set; }
    public DbSet<FooBar> FooBars { get; set; }
}





现在查看您的查看问题。总的来说,在EF中复制视图的方式是创建ViewModel,它们是未映射到数据库但可以从POCO类构建的POCO,或者,如果您喜欢,可以从DbContext构建。





Now for your view question. By and large, the way that you replicate views in EF is to create ViewModels, which are POCOs that are not mapped to the database but can be constructed from either POCO classes or, if you're fancy, from the DbContext.

public class FooBarViewModel
{
    public List<string> FooProp1 { get; set; }
    public List<string> BarProp1 { get; set; }

    public FooBarViewModel(MyContext context)
    {
        FooProp1 = context.Foos.Select(x => x.Prop1).toList();
        BarProp1 = context.Bars.Select(x => x.Prop1).toList();
    }
}

public class Foo
{
    public int Id { get; set; }
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
}

public class Bar 
{
    public int Id { get; set; }
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
}

public class MyContext : DbContext
{
    public DbSet<Foo> Foos { get; set; }
    public DbSet<Bar> Bars { get; set; }
}





希望这会有所帮助!



Hope this helps!


这篇关于如何在EF中制作视图。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆