简体   繁体   English

为什么此代码适用于 SQL Server Express 但*不适用于* SQL Server CE?

[英]Why this code works for SQL Server Express but *not* SQL Server CE?

I get a NullReferenceException when my target provider is SQL Server CE, but with SQL Server Express it works.当我的目标提供程序是 SQL Server CE 时,我得到一个NullReferenceException ,但是对于 SQL Server Express 它可以工作。 I get the exception on the return... when I'm trying to access the Comments .当我试图访问Comments时,我在return...时得到了异常。 Model is very simple and it can be inferred from the code. Model很简单,从代码中可以推断出来。 Thanks for any help.谢谢你的帮助。 I'm using Entity Framework code-first 4.3 and SQL Server CE 4.0 by the way.顺便说一下,我使用的是 Entity Framework code-first 4.3 和 SQL Server CE 4.0。

  • using the same code with SQL Server Express as provider = runs fine使用与 SQL Server Express 相同的代码作为提供商 = 运行良好

  • using the same code with SQL Server CE as provider = exception error使用与 SQL 服务器 CE 相同的代码作为提供者 = 异常错误

In addition, I have no problems with SQL Server CE.另外我用SQL Server CE没问题。 It works on my machine and I have done single-table operations with Entity Framework code-first and even asp.net authorization.它在我的机器上运行,我已经使用 Entity Framework code-first 甚至 asp.net 授权完成了单表操作。 I have never done any nested addition/deletion like this on related entities though so I'm curious as to why this doesn't work..我从来没有在相关实体上做过任何像这样的嵌套添加/删除,所以我很好奇为什么这不起作用..

            db.Persons.Add(new Person()
            {
                FullName = "JC Viray",
                Comments = new List<Comment>(){
                         new Comment(){CommentContent="Hello!"},
                         new Comment(){CommentContent="World!"}
            }
            });

            db.SaveChanges();

            return db.Persons.FirstOrDefault().Comments.FirstOrDefault().CommentContent;

What stumps me also is that in SQL Server Express, if I examine the data, the comments are there.让我感到困惑的是,在 SQL Server Express 中,如果我检查数据,评论就在那里。

In SQL Server CE, only the Persons table has data while the Comments table is empty.在SQL Server CE中,只有Persons表有数据, Comments表是空的。

EDIT:编辑:

If it helps, here is the model:如果有帮助,请拨打 model:

    public class Person
{
    [Key]
    public int PersonId { get; set; }
    public string FullName { get; set; }

    public virtual ICollection<Comment> Comments { get; set; }
}

public class Comment
{
    [Key]
    public int CommentId { get; set; }
    public string CommentContent { get; set; }

    [ForeignKey("Person")]
    public int PersonId { get; set; }
    public virtual Person Person { get; set; }
}

The problem is that: SQL CE error details are a lot more vague than when using SQL Server Express as provider therefore the confusion and the vague direction.问题是: SQL CE 错误详细信息比使用 SQL Server Express 作为提供者时更加模糊,因此混乱和方向模糊。

SQLCE Exception details is something like: SQLCE 异常详细信息类似于:

NullException空异常

SQLExpress Exception details is something like: SQLExpress 异常详细信息类似于:

The model backing the 'MyContext' context has changed since the database was created.自数据库创建以来,支持“MyContext”上下文的 model 已更改。 Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).考虑使用代码优先迁移来更新数据库 (http://go.microsoft.com/fwlink/?LinkId=238269)。

The solution is: to sync the models by adding this code:解决方案是:通过添加以下代码来同步模型:

Database.SetInitializer(new DropCreateDatabaseAlways<MyContext>());

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

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