0

我使用 EF 4。拥有此代码来测试我的模型的功能。

    using (var context = new CamelotDB())
    {
        Console.WriteLine("Starting User Repository Testing{0}",Environment.NewLine);
        Console.WriteLine("Creating Test User");
        DomainEntity userToAdd = new User()
        {
            EntityName = "Test User",
            EntityType = DomainEntity.eEntityType.User,
            Username = "TestUser",
            Password = "123",
            Email = "TestUser@Test.org",
            FirstName = "Test",
            LastName = "User",
            Phone = "123456789",
            EntityParentID = null,
        };

        Console.WriteLine("Saving user to database...");
        userToAdd = context.DomainEntities.Add(userToAdd);
        context.SaveChanges();

        Console.WriteLine("Fetching user from database...");
        int userID = userToAdd.EntityID;
        DomainEntity userToDelete = context.DomainEntities.Find(userID);

        Console.WriteLine("Removing the user...");
        context.DomainEntities.Remove(userToDelete);
        context.SaveChanges();

    } 

用户成功添加到数据库,我取回用户 ID。当我尝试使用该Find()方法时

DomainEntity userToDelete = context.DomainEntities.Find(userID);

我收到以下错误:值不能为空。参数名称键

我事件试图调用以下内容:

DomainEntity userToDelete = context.DomainEntities.Find(2);

知道用户 ID 2 存在于数据库中,但仍然得到相同的异常。

4

1 回答 1

0

以某种方式修复它...只是从 EDMX 中删除了关键属性并再次添加它以保存更改,一切都恢复正常猜测这与 EDMX 生成有关...

于 2013-01-04T05:52:51.840 回答