错误" NullReferenceException"保存EF6 for VB中的更改时收到

时间:2015-03-10 18:53:33

标签: vb.net entity-framework entity-framework-6 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:对象引用未设置为对象的实例。”

当我将鼠标悬停在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帖子,但没有人为我解决了这个问题。 Waldo在哪里?

1 个答案:

答案 0 :(得分:1)

Blogs不是VB代码中的属性,应该是:

Public Property Blogs As DbSet(Of Blog)

没有&#34;财产&#34;它只是一个成员变量,因此DbContext不会自动初始化。

相关问题