在 EF6 中保存 VB 的更改时收到错误 "NullReferenceException"
Error "NullReferenceException" received when saving changes in EF6 for VB
这是控制台应用 VB.NET 代码:
Imports System.Data.Entity
Imports System.Linq
Imports System.Configuration
Module Module1
Sub Main()
Using db = New BloggingContext()
Console.Write("Enter a name for a new Blog: ")
Dim name = Console.ReadLine()
Dim blogt As New Blog()
With blogt
.Name = name
End With
db.Blogs.Add(blogt)
db.SaveChanges()
Dim query = From b In db.Blogs
Order By b.Name
Console.WriteLine("All blogs in the database:")
For Each item In query
Console.WriteLine(item.Name)
Next
Console.WriteLine("Press any key to exit...")
Console.ReadKey()
End Using
End Sub
Public Class Blog
Public Property BlogId() As Integer
Public Property Name() As String
End Class
Public Class BloggingContext
Inherits DbContext
Public Sub New()
MyBase.New("dbConnString")
End Sub
Public Blogs As DbSet(Of Blog)
End Class
End Module
我的错误指向 Main() 中的这一行 'db.Blogs.Add(blogt)' 指出 "System.NullReferenceException: Object reference not set to an instance of an object."
当我将鼠标悬停在 db.Blogs.Add(blogt) 上时,它告诉我 db.Blogs 什么都不是。我将相同的代码转换为 C# 并且运行良好:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
namespace CodeFirstNewDatabaseSample
{
class Program
{
static void Main(string[] args)
{
using (var db = new BloggingContext())
{
// Create and save a new Blog
Console.Write("Enter a name for a new Blog: ");
var name = Console.ReadLine();
var blog = new Blog { Name = name };
db.Blogs.Add(blog);
db.SaveChanges();
// Display all Blogs from the database
var query = from b in db.Blogs
orderby b.Name
select b;
Console.WriteLine("All blogs in the database:");
foreach (var item in query)
{
Console.WriteLine(item.Name);
}
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
}
public class Blog
{
public int BlogId { get; set; }
public string Name { get; set; }
public virtual List<Post> Posts { get; set; }
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public virtual Blog Blog { get; set; }
}
public class BloggingContext : DbContext
{
public BloggingContext()
: base("dbConnString")
{
//Database.SetInitializer<BloggingContext>(new CreateDatabaseIfNotExists<BloggingContext>());
}
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
}
}
不确定缺少什么。我已经阅读了所有其他关于尝试 SaveChanges 时出现 NullReferenceException 错误的 EF 帖子,但是 none 已经为我解决了这个问题。 Waldo 在哪里?
Blogs
不是VB代码中的属性,应该是:
Public Property Blogs As DbSet(Of Blog)
没有"Property"它只是一个成员变量,所以它不会被DbContext
自动初始化。
这是控制台应用 VB.NET 代码:
Imports System.Data.Entity
Imports System.Linq
Imports System.Configuration
Module Module1
Sub Main()
Using db = New BloggingContext()
Console.Write("Enter a name for a new Blog: ")
Dim name = Console.ReadLine()
Dim blogt As New Blog()
With blogt
.Name = name
End With
db.Blogs.Add(blogt)
db.SaveChanges()
Dim query = From b In db.Blogs
Order By b.Name
Console.WriteLine("All blogs in the database:")
For Each item In query
Console.WriteLine(item.Name)
Next
Console.WriteLine("Press any key to exit...")
Console.ReadKey()
End Using
End Sub
Public Class Blog
Public Property BlogId() As Integer
Public Property Name() As String
End Class
Public Class BloggingContext
Inherits DbContext
Public Sub New()
MyBase.New("dbConnString")
End Sub
Public Blogs As DbSet(Of Blog)
End Class
End Module
我的错误指向 Main() 中的这一行 'db.Blogs.Add(blogt)' 指出 "System.NullReferenceException: Object reference not set to an instance of an object."
当我将鼠标悬停在 db.Blogs.Add(blogt) 上时,它告诉我 db.Blogs 什么都不是。我将相同的代码转换为 C# 并且运行良好:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
namespace CodeFirstNewDatabaseSample
{
class Program
{
static void Main(string[] args)
{
using (var db = new BloggingContext())
{
// Create and save a new Blog
Console.Write("Enter a name for a new Blog: ");
var name = Console.ReadLine();
var blog = new Blog { Name = name };
db.Blogs.Add(blog);
db.SaveChanges();
// Display all Blogs from the database
var query = from b in db.Blogs
orderby b.Name
select b;
Console.WriteLine("All blogs in the database:");
foreach (var item in query)
{
Console.WriteLine(item.Name);
}
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
}
public class Blog
{
public int BlogId { get; set; }
public string Name { get; set; }
public virtual List<Post> Posts { get; set; }
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public virtual Blog Blog { get; set; }
}
public class BloggingContext : DbContext
{
public BloggingContext()
: base("dbConnString")
{
//Database.SetInitializer<BloggingContext>(new CreateDatabaseIfNotExists<BloggingContext>());
}
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
}
}
不确定缺少什么。我已经阅读了所有其他关于尝试 SaveChanges 时出现 NullReferenceException 错误的 EF 帖子,但是 none 已经为我解决了这个问题。 Waldo 在哪里?
Blogs
不是VB代码中的属性,应该是:
Public Property Blogs As DbSet(Of Blog)
没有"Property"它只是一个成员变量,所以它不会被DbContext
自动初始化。