简体   繁体   English

实体框架DbSet抛出NullReferenceException

[英]Entity Framework DbSet throws NullReferenceException

I try to post some values into my mysql database with the entity framework . 我尝试mysql database with the entity framework将一些值发布到我的mysql database with the entity framework I'm able to connect to my database successfully. 我能够成功连接到我的数据库。 But, after I try to add some values, I've got an NullReferenceException error. 但是,在尝试添加一些值之后,出现了NullReferenceException错误。

Here's my code: 这是我的代码:

public class MysqlContext: DbContext
{
    public DbSet<Test> Test { get; set; }

    public MysqlContext(): base("MysqlDefaultConnection")
    {
        Database.Connection.Open();
        var t = new Test { Name = "test", Id = 0 };
        Test.Add(t);
        SaveChanges();
    }
}

[Table("test")]
public class Test
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
}

And this is my configuration of my entity framework: 这是我对实体框架的配置:

<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>

<connectionStrings>
<add name="MysqlDefaultConnection" connectionString="server=localhost;user id=dev;password=****;database=****" providerName="MySql.Data.MySqlClient" />

<entityFramework>
<defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6" />
<providers>
  <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
</providers>

Finally this is my table description from my database: 最后,这是我对数据库的表描述:

CREATE TABLE `test` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`Name` varchar(45) NOT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

So, if I try to connect to my database, everything is okay. 因此,如果我尝试连接到数据库,则一切正常。 After I try to add my entity into the DbSet I'll get an exception. 在尝试将我的实体添加到DbSet中之后,我将获得一个异常。

It behaves the same way if you try this code not in the DbContext constructor? 如果您不在DbContext构造函数中尝试此代码,其行为方式相同。

public class MysqlContext: DbContext
{
    public DbSet<Test> Test { get; set; }

    public MysqlContext(): base("MysqlDefaultConnection")
    {

    }
}

[Table("test")]
public class Test
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
}

public void Main()
{
  var myContext = new MysqlContext();
  var t = new Test { Name = "test", Id = 0 };    
  myContext.Set<Test>.Add(t);
  myContext.SaveChanges();
}

I believe this is because you are doing this in the contractor. 我相信这是因为您正在承包商中进行此操作。 At this stage I would assume Test property is not initialised to a value yet. 在此阶段,我假设Test属性尚未初始化为值。 Try run your test code later in the application lifecycle after MysqlContext has been initialized. 初始化MysqlContext之后,请尝试在应用程序生命周期中稍后运行测试代码。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM