具有动态模式选择的实体框架 [英] Entity Framework with dynamic schema selection

查看:65
本文介绍了具有动态模式选择的实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SQL Server数据库中为每个用户提供了不同的架构。使用Entity Framework,我必须将记录添加到用户特定的模式表中。



现在我的数据库如下所示。

这里S1,S2,S3是架构。



    S1.T1,S2.T1和S3.T1 等等。


    DbSchemaEntities db = new DbSchemaEntities();

            

    db.T1.Add(new T1(){Name =" S1Data"});

    db.T1.Add(new T1(){Name =" S2Data"});



    db.T1.Add(new T1(){Name =" SxData"});

    

    db.SaveChanges();
$


以上代码仅将数据插入到模式S1中。



在我的`.edmx`文件中,xml包含以下内容:



  &NBSP;  < EntityContainer Name =" DbSchemaModelStoreContainer">

  &NBSP; &NBSP; &NBSP; &NBSP; < EntitySet Name =" T1"的EntityType = QUOT; Self.T1"架构= QUOT; S1"商店:种类="表" />

  &NBSP;  < / EntityContainer>
$


如何在运行时动态选择架构?

I have different schema for each user in a SQL Server database. With Entity Framework, I have to add record in to the user specific schema tables.

Right now my db is like below.
Here S1, S2, S3 are the schemas.

    S1.T1, S2.T1 and S3.T1 and so on

    DbSchemaEntities db = new DbSchemaEntities();
            
    db.T1.Add(new T1() {Name="S1Data"});
    db.T1.Add(new T1() {Name="S2Data"});

    db.T1.Add(new T1() {Name="SxData"});
    
    db.SaveChanges();

The above code only inserting the data in to the schema S1 only.

In my `.edmx` file, the xml contains the following:

     <EntityContainer Name="DbSchemaModelStoreContainer">
          <EntitySet Name="T1" EntityType="Self.T1" Schema="S1" store:Type="Tables" />
     </EntityContainer>

How can I dynamically choose the schema at run time?

推荐答案

您好SairamVinnakota,

Hi SairamVinnakota,

>>如何在运行时动态选择架构?

>>How can I dynamically choose the schema at run time?

我们无法动态使用它,因为实体框架首先映射数据库,并使用固定的映射关系,我们无法动态映射它。如果想动态使用它,我建议您可以使用原始SQL来实现它。像这样:

We could not use it dynamically, because entity framework map the database first, and use the fixed mapping relationship, we could not map it dynamic. if want to use it dynamically, I would suggest that you could use raw SQL to achieve it. like this:

var query = db.Database.SqlQuery<YourCustomClass>("select S1.T1, S2.T1,S3.T1 from xxx");

有关详细信息,请参阅:

For more information, please refer to:

https://msdn.microsoft.com/en-us/library/jj592907(v = vs.113)的.aspx

祝你好运,

张龙武


这篇关于具有动态模式选择的实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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