Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You don't need to do anything special. Classes are always nullable.</p> <p>I just tried this (with MVC3):</p> <p>In my Models directory:</p> <pre><code>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&lt;Book&gt; Loans { get; set; } } public class BookContext : System.Data.Entity.DbContext { public System.Data.Entity.DbSet&lt;Book&gt; Books { get; set; } public System.Data.Entity.DbSet&lt;Loaner&gt; Loaners { get; set; } } } </code></pre> <p>In my HomeController:</p> <pre><code>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(); } } } </code></pre> <p>The connectionstring in Web.Config:</p> <pre><code>&lt;add name="BookContext" connectionString="Data Source=|DataDirectory|BookContext.sdf" providerName="System.Data.SqlServerCe.4.0" /&gt; </code></pre> <p>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:</p> <pre><code>ID: 1; Title: "New Title"; Author: "New Author"; ISBN: "New ISBN"; LoanerID: null </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload