简体   繁体   English

值不能为空。 参数名称connectionString

[英]Value cannot be null. Parameter name connectionString

Hi guys I'm having trouble saving my data to DB. 嗨,大家好,我无法将数据保存到数据库。 I'm using EF and .net core . 我正在使用EF.net core I have provide my sample code below: 我在下面提供了示例代码:

What's actually wrong here? 这里到底有什么问题? I provided services.AddDbContext in ConfigureServices for MyContext. 我公司提供services.AddDbContextConfigureServices为MyContext。

public class MyContext: DbContext
{
    private IConfigurationRoot _config;

    public MyContext(IConfigurationRoot config)
    {
       this._config = config;
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        base.OnConfiguring(optionsBuilder);
        optionsBuilder.UseSqlServer(_config["Data:ConnectionString"]);
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        ...
    }
}

appsettings.json appsettings.json

{
  "Data": {
    "ConnectionString": "Data Source=MyDataSource;Initial Catalog=MyCatalog;Trusted_Connection=True"},

Another Class: 另一类:

public class AnotherClass
{ 
     private MyContext mycontext;
     private IConfigurationRoot configurationRoot;

     public AnotherClass()
     { 
        var builder = new ConfigurationBuilder();
        configurationRoot = builder.Build();
        mycontext = new MyContext(configurationRoot);
     }

     public MyMethod()
     {
        try
        {
            //object to be saved using EF
            var saveObj = new SaveObj()
            {
             //pretend to have initialize some object here to save
            };

            mycontext.SaveObj.Add(saveObj);
            mycontext.SaveChanges();
        }
        catch(Exception e)
        {
           //Throws an exception. Value cannot be null. Parameter name: connectionString
        }
    }
}

Check this out: Value cannot be null. 签出: 值不能为null。 Parameter name: connectionString appsettings.json in starter 参数名称:starter中的connectionString appsettings.json

I think your appsettings.json should look like this: 我认为您的appsettings.json应该看起来像这样:

{
  "ConnectionStrings": {
    "DefaultConnection": "Data Source=MyDataSource;Initial Catalog=MyCatalog;Trusted_Connection=True"
  }
}

Skip the "Data" part. 跳过“数据”部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ArgumentNullException:值不能为空。 参数名称:connectionString - ArgumentNullException: Value cannot be null. Parameter name: connectionString 值不能为空。 (参数'connectionString') - Value cannot be null. (Parameter 'connectionString') Asp.Net Core 2.0 ArgumentNullException:值不能为null。 参数名称:connectionString - Asp.Net Core 2.0 ArgumentNullException: Value cannot be null. Parameter name: connectionString .NET 核心工作者异常:值不能是 null。 参数名称连接字符串 - .NET Core worker exception: Value cannot be null. Parameter name connectionString 获取连接字符串时出错:ArgumentNullException: Value cannot be null。 参数名称:连接字符串 - Error when get connection string: ArgumentNullException: Value cannot be null. Parameter name: connectionString System.ArgumentNullException:值不能为 null。(参数“connectionString”) - System.ArgumentNullException: Value cannot be null. (Parameter 'connectionString') 值不能为null。参数名称:String - Value cannot be null. Parameter name: String 值不能为空。 参数名称:路径 - Value cannot be null. Parameter name: path 值不能是 null。 参数名称:dest - Value cannot be null. Parameter name: dest 值不能为空。 参数名称:extent - Value cannot be null. Parameter name: extent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM