所有代码如下:
public class Book
{
public int BookId { get; set; }
public string BookTitle { get; set; }
public Pulisher Pulisher { get; set; }
}
[ComplexType]
public class Pulisher
{
public string PulisherName { get; set; }
public string PulisherAddress { get; set; }
}
public class BookDbContext : DbContext
{
public BookDbContext() : base("BookDb") {
Database.SetInitializer<BookDbContext>(
new DropCreateDatabaseIfModelChanges<BookDbContext>()
);
}
public IDbSet<Book> Books { get; set; }
}
-----------------------------------------------------------------------
public static Models.BookDbContext db = new Models.BookDbContext();
static void Main(string[] args)
{
Models.Book book = new Models.Book
{
BookTitle = "EF4.1 STEP BY STEP",
Pulisher = new Models.Pulisher
{
PulisherName = "机械工业",
PulisherAddress = "成都"
}
};
db.Books.Add(book);
db.SaveChanges();
}
数据库中没有BookDB数据库, 也没有连接字符串。 没有数据库,我也不知道怎么写连接字符串, 呵呵。 这个问题怎么解决啊。
在app.config或web.config中加上连接字符串配置:
<add name="BookDb" connectionString="" providerName="System.Data.SqlClient"/>