如何使用C#实体框架 [英] how to use C# entity framework

查看:155
本文介绍了如何使用C#实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是实体框架的新手,我想学习和使用实体框架。



我的问题是:



我希望我的用户能够输入连接字符串,那么如何编写实体框架连接字符串动态?如何用实体框架查询动态连接字符串?如果我有程序,如何使用实体框架动态连接字符串的程序?



谢谢你浪费时间阅读我的问题。

I'm the new for entity framework and I want to learn and use the entity framework.

my Question is:

I want my user be able input connection string so how to write entity framework connection string dynamic? how to Query with entity framework dynamic connection string? If I have procedure, how to use procedure with entity framework dynamic connection string?

thank you that you waste your time for reading my Question.

推荐答案

1.设置动态Entity Framework的连接字符串(简称EF),假设您的 ObjectContext 类名为MyDataContext,您必须使用其构造函数接受连接字符串作为参数,如下例所示;



2.要使用此动态连接字符串查询EF,您必须使用下一个示例中的代码:

1.To setup dynamic connection string for Entity Framework (shortly EF), supposing that your ObjectContext class is named MyDataContext, you have to use its constructor that accept a connection string as parameter like in the example below;

2.To query EF by using this dynamic connection string you have to use the code like in the next example:
Student student = null;
using (MyDataContext dataContext = new MyDataContext(yourConnectionString))
                    {
                        var query = from st in dataContext .Students
                            where st.StudentName == "Bill"
                            select st;
                        // 
                        student = query.FirstOrDefault();  
                    }



3.有关EF的更多详细信息,包括在EF中使用字符串过程,请参阅下一个站点中的详细信息:http://www.entityframeworktutorial.net/stored-procedure-in-entity-framework.aspx [ ^ ]


这篇关于如何使用C#实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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