EF DbContext 读取 appcongfig 问题

时间:2022-12-06 23:43:50
我在使用 EF 4.2 的DbContext.Set<T>().Create()方法时 系统报出No connection string named 'DataContainer' could be found in the application config file.的错误

我的代码如下

namespace DataCommand
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    using DataModel;
    public partial class DataContainer : DbContext
    {
        public DataContainer()
            : base("name=DataContainer")
        {
        }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }
    
        public DbSet<Members> Members { get; set; }
        public DbSet<MemLoginLog> MemLoginLog { get; set; }
        public DbSet<MemBlackList> MemBlackList { get; set; }
    }
}


appconfig:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="DataContainer" connectionString="metadata=res://*/Data.csdl|res://*/Data.ssdl|res://*/Data.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=***.***.***.***;initial catalog=DataCenter;user id=********;password=********;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>


找不到解决的头绪
求帮助

7 个解决方案

#1


我这里应该是可以的, 基本是同样的代码
winform程序
App.config:


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
  <add name="DataContainer" connectionString="metadata=res://*/Data.csdl|res://*/Data.ssdl|res://*/Data.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=***.***.***.***;initial catalog=DataCenter;user id=********;password=********;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>


ef context:

namespace WindowsFormsApplication1
{
    public partial class DataContainer : DbContext
    {
        public DataContainer()
            : base("name=DataContainer")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public DbSet<Members> Members { get; set; }
        public DbSet<MemLoginLog> MemLoginLog { get; set; }
        public DbSet<MemBlackList> MemBlackList { get; set; }
    }

    public class Members
    {
        
    }
    public class MemLoginLog
    {
    }

    public class MemBlackList
    {
    }


winform:


private void button1_Click(object sender, EventArgs e)
        {
            DataContainer con = new DataContainer();
            con.Set<Members>().Create();
         }


错误是"Unable to load the specified metadata resource.",估计是没有数据库的原因。

如果将App.config中的Name 改成DataContainer222,编译后运行。错误是"No connection string named 'DataContainer' could be found in the application config file."和你的一样。

#2


我觉得还是配置文件没弄好。
winform,wpf,windows service等工程(这类工程都生成exe)的App.config会在编译后变成<程序名>.exe.config,你要查看那个config文件是否正确。

dll的话配置需要放在相应exe工程的App.config中。

web程序的配置文件是web.config.

#3


引用 2 楼 findcaiyzh 的回复:
我觉得还是配置文件没弄好。
winform,wpf,windows service等工程(这类工程都生成exe)的App.config会在编译后变成<程序名>.exe.config,你要查看那个config文件是否正确。

dll的话配置需要放在相应exe工程的App.config中。

web程序的配置文件是web.config.


是的 我把DBContext 放到Dal的类库内 用的是app.config

#4


你放到其他工程里去了吧  编译的时候运行的CONFIG是你首启动项的工程项目。。。。你最好在你的运行工程里边检查一下CONFIG文件

#5


引用 3 楼 sindevil 的回复:
是的 我把DBContext 放到Dal的类库内 用的是app.config


DBContext 放到Dal的类库内  

这是没有问题的,但是得把配置放在exe项目的app.config中,或者web.config中。

#6


这些怎么都不知道了 EF DbContext 读取 appcongfig 问题

#7


你的连接字符串格式不正确。
根据你提供的代码,使用的EF CodeFirst,连接字符串的格式应该是:

<connectionStrings>
  <add name="DataContainer" connectionString="data source=***.***.***.***;initial catalog=DataCenter;user id=********;password=********;multipleactiveresultsets=True;App=EntityFramework" providerName="System.Data.EntityClient" />
  </connectionStrings>

#1


我这里应该是可以的, 基本是同样的代码
winform程序
App.config:


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
  <add name="DataContainer" connectionString="metadata=res://*/Data.csdl|res://*/Data.ssdl|res://*/Data.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=***.***.***.***;initial catalog=DataCenter;user id=********;password=********;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>


ef context:

namespace WindowsFormsApplication1
{
    public partial class DataContainer : DbContext
    {
        public DataContainer()
            : base("name=DataContainer")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public DbSet<Members> Members { get; set; }
        public DbSet<MemLoginLog> MemLoginLog { get; set; }
        public DbSet<MemBlackList> MemBlackList { get; set; }
    }

    public class Members
    {
        
    }
    public class MemLoginLog
    {
    }

    public class MemBlackList
    {
    }


winform:


private void button1_Click(object sender, EventArgs e)
        {
            DataContainer con = new DataContainer();
            con.Set<Members>().Create();
         }


错误是"Unable to load the specified metadata resource.",估计是没有数据库的原因。

如果将App.config中的Name 改成DataContainer222,编译后运行。错误是"No connection string named 'DataContainer' could be found in the application config file."和你的一样。

#2


我觉得还是配置文件没弄好。
winform,wpf,windows service等工程(这类工程都生成exe)的App.config会在编译后变成<程序名>.exe.config,你要查看那个config文件是否正确。

dll的话配置需要放在相应exe工程的App.config中。

web程序的配置文件是web.config.

#3


引用 2 楼 findcaiyzh 的回复:
我觉得还是配置文件没弄好。
winform,wpf,windows service等工程(这类工程都生成exe)的App.config会在编译后变成<程序名>.exe.config,你要查看那个config文件是否正确。

dll的话配置需要放在相应exe工程的App.config中。

web程序的配置文件是web.config.


是的 我把DBContext 放到Dal的类库内 用的是app.config

#4


你放到其他工程里去了吧  编译的时候运行的CONFIG是你首启动项的工程项目。。。。你最好在你的运行工程里边检查一下CONFIG文件

#5


引用 3 楼 sindevil 的回复:
是的 我把DBContext 放到Dal的类库内 用的是app.config


DBContext 放到Dal的类库内  

这是没有问题的,但是得把配置放在exe项目的app.config中,或者web.config中。

#6


这些怎么都不知道了 EF DbContext 读取 appcongfig 问题

#7


你的连接字符串格式不正确。
根据你提供的代码,使用的EF CodeFirst,连接字符串的格式应该是:

<connectionStrings>
  <add name="DataContainer" connectionString="data source=***.***.***.***;initial catalog=DataCenter;user id=********;password=********;multipleactiveresultsets=True;App=EntityFramework" providerName="System.Data.EntityClient" />
  </connectionStrings>