aroshanzamir
پنج شنبه 21 فروردین 1393, 23:35 عصر
سلام دوستان
من می خواهم با روش Code First کار کنم ولی همش بهم خطا میده ..
1- پکیج Entity را اضاف می کنم
2-کلاس ها را می نویسم
public class Blog
{
public int Id { get; set; }
public string Name { get; set; }
public virtual List<Post> Posts { get; set; }
public Blog()
{
Posts = new List<Post>();
}
}
public class Post
{
public int Id { get; set; }
public string Name { get; set; }
public virtual Blog Blog { get; set; }
}
public class DataCollection:DbContext
{
public DataCollection() : base("DataCollection") { }
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Postes { get; set; }
}
و فایل وب کانفیگ :
<connectionStrings>
<add name="DataCollection"
connectionString="Data Source=.;Initial Catalog=ServiceAWModel;Integrated Security=True" />
</connectionStrings>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrame workSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectio nFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
و این هم کد برنامه :
using(var db=new DataCollection())
{
Blog _blog = new Blog() { Name="Math" };
_blog.Posts.Add(new Post { Name="Hello" });
_blog.Posts.Add(new Post { Name="Hi"});
db.Blogs.Add(_blog);
db.SaveChanges();
}
در ضمن در پایگاه داده دیتابیس هم ایجاد کردم ....باز هم خطا داره
ممنون ....
من می خواهم با روش Code First کار کنم ولی همش بهم خطا میده ..
1- پکیج Entity را اضاف می کنم
2-کلاس ها را می نویسم
public class Blog
{
public int Id { get; set; }
public string Name { get; set; }
public virtual List<Post> Posts { get; set; }
public Blog()
{
Posts = new List<Post>();
}
}
public class Post
{
public int Id { get; set; }
public string Name { get; set; }
public virtual Blog Blog { get; set; }
}
public class DataCollection:DbContext
{
public DataCollection() : base("DataCollection") { }
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Postes { get; set; }
}
و فایل وب کانفیگ :
<connectionStrings>
<add name="DataCollection"
connectionString="Data Source=.;Initial Catalog=ServiceAWModel;Integrated Security=True" />
</connectionStrings>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrame workSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectio nFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
و این هم کد برنامه :
using(var db=new DataCollection())
{
Blog _blog = new Blog() { Name="Math" };
_blog.Posts.Add(new Post { Name="Hello" });
_blog.Posts.Add(new Post { Name="Hi"});
db.Blogs.Add(_blog);
db.SaveChanges();
}
در ضمن در پایگاه داده دیتابیس هم ایجاد کردم ....باز هم خطا داره
ممنون ....