Keyword not supported: 'metadata'.? with Sql Connection in Entityt Framework with MVC3

后端 未结 4 1709
时光取名叫无心
时光取名叫无心 2020-12-15 19:51

I am using Entity Framework 4 with my Asp.Net MVC3 application. My problem is that I am using Entity Framework to perform action with my database , That i

4条回答
  •  抹茶落季
    2020-12-15 20:42

    Another option (other than 2 separate connection strings for the same thing) is to build a method that returns an ADO.NET connection string from your Entity Framework object:

    using System.Data.EntityClient;
    using System.Data.SqlClient;
    ...
    private string GetADOConnectionString()
    {
        SalesSyncEntities ctx = new SalesSyncEntities(); //create your entity object here
        EntityConnection ec = (EntityConnection)ctx.Connection;
        SqlConnection sc = (SqlConnection)ec.StoreConnection; //get the SQLConnection that your entity object would use
        string adoConnStr = sc.ConnectionString;
        return adoConnStr;
    }
    

    (I place this somewhere in my Class Library where my edmx files are)

    (I got this from http://justgeeks.blogspot.com/2009/11/getting-sqlconnection-from.html)

    Or even better... if your SQLConnection stuff are manual SQL queries, skip the SQLConnection entirely via the ExecuteStoredCommand:

    new AdventureEntities().ExecuteStoreCommand(
            @"    UPDATE Users
                  SET lname = @lname 
                  WHERE Id = @id",
            new SqlParameter("lname", lname), new SqlParameter("id", id));
    

    (I got this from Entity Framework getting an sql connection)

提交回复
热议问题