如何在EF Codefirst中创建可为空的属性? [英] How to make a nullable property in EF Codefirst?
问题描述
我的书架测试应用程序中有两个POCO:
I have two POCOs in my "Bookshelf" test application:
/// <summary>
/// Represents a book
/// </summary>
public class Book
{
public int ID { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public string ISBN { get; set; }
public virtual Loaner LoanedTo { get; set; }
}
/// <summary>
/// Represents a Loaner
/// </summary>
public class Loaner
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Book> Loans { get; set; }
}
我的LoanedTo是否可以为空?我的意思是说书并不总是借来的,对!我尝试过
Is there a way that my LoanedTo could be nullable? I mean a book isn't always loaned, right! I tried
public virtual Loaner? LoanedTo { get; set; }
但我得到:
类型'RebtelTests.Models.Loaner'必须为非空值类型,以便在通用类型或方法 System.Nullable中将其用作参数 T
But I get: The type 'RebtelTests.Models.Loaner' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable'
因此,我在某处一定在想错,但是我不知道。
So I must be thinking wrong somewhere, but I can't figure it out. Probably easy squeeze for you guys.
推荐答案
您不需要做任何特别的事情。类始终是可为空的。
You don't need to do anything special. Classes are always nullable.
我刚刚尝试了此操作(使用MVC3):
I just tried this (with MVC3):
在我的模型目录中:
namespace MvcApplication2.Models
{
public class Book
{
public int ID { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public string ISBN { get; set; }
public virtual Loaner LoanedTo { get; set; }
}
public class Loaner
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Book> Loans { get; set; }
}
public class BookContext : System.Data.Entity.DbContext
{
public System.Data.Entity.DbSet<Book> Books { get; set; }
public System.Data.Entity.DbSet<Loaner> Loaners { get; set; }
}
}
在我的HomeController中:
In my HomeController:
namespace MvcApplication2.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
string message = "OK";
try
{
var context = new Models.BookContext();
var book = new Models.Book();
book.Title = "New Title";
book.Author = "New Author";
book.ISBN = "New ISBN";
context.Books.Add(book);
context.SaveChanges();
}
catch (Exception err)
{
message = err.ToString();
}
ViewBag.Message = message;
return View();
}
}
}
连接字符串在Web.Config中:
The connectionstring in Web.Config:
<add name="BookContext" connectionString="Data Source=|DataDirectory|BookContext.sdf" providerName="System.Data.SqlServerCe.4.0" />
运行应用程序时,视图显示为 OK。这意味着不会引发任何异常。当我查看App_Data文件夹时,已经创建了一个BookContext.sdf文件。该数据库包含有关帐簿和贷款人的表格。贷款人表格为空。书中的一条包含一条记录:
When I run the application, the view displays "OK". This means that no exception was thrown. When I look in my App_Data folder, a BookContext.sdf file has been created. That database contains a table for the Books and the Loaners. The table for the Loaners is empty. The one for the Books contains one record:
ID: 1; Title: "New Title"; Author: "New Author"; ISBN: "New ISBN"; LoanerID: null
这篇关于如何在EF Codefirst中创建可为空的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!