小编典典

值不能为空。参数名称:来源

c#

这很可能是我浪费了很长时间解决的时间浪费最大的问题。

var db = new hublisherEntities();
establishment_brands est = new establishment_brands();

est.brand_id = 1;
est.establishment_id = 1;
est.price = collection["price"];
est.size = collection["size"];

db.establishment_brands.Add(est);
db.SaveChanges();

这给我一个错误

值不能为空。参数名称:来源

的堆栈跟踪

[ArgumentNullException:值不能为null。参数名称:source]
System.Linq.Enumerable.Any(IEnumerable 1 source, Func2谓词)+4083335
System.Data.Entity.Internal.InternalContext.WrapUpdateException(UpdateException
updateException)+87
System.Data.Entity.Internal.InternalContext.SaveChanges()+ 193
System.Data.Entity.Internal.LazyInternalContext.SaveChanges()+33
System.Data.Entity.DbContext.SaveChanges()+20 … …

我只想向表添加一个实体。ORM是EF。


阅读 990

收藏
2020-05-19

共1个答案

小编典典

我前一段时间有这个,答案不一定是您期望的。当您的连接字符串错误时,通常会出现此错误消息。

大概,您需要这样的东西:

<connectionStrings>
    <add name="hublisherEntities" connectionString="Data Source=localhost;Initial Catalog=hublisher;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
        <parameters>
            <parameter value="Data Source=localhost;Initial Catalog=hublisher;Integrated Security=True" />
        </parameters>
    </defaultConnectionFactory>
</entityFramework>

发生的事情是它在错误的位置寻找数据源。实体框架对它的指定稍有不同。如果您发布连接字符串和EF配置,那么我们可以进行检查。

2020-05-19