Entity framework 每个类的实体框架类型:C,访问不是另一个类的导航属性的子类
有许多C/Entity框架示例可以查询和访问一个类的属性,其中该类是一个属性,而不是另一个类的子类 所以这里有一个问题,关于如何使用Entity Framework查询并访问SQL server中任何类的导航属性子类 关于如何查询和访问不是任何类的导航属性的子类,我也找不到任何示例 我只是不明白为什么微软没有这样一个基本的例子 //问题:如何使用select创建查询以访问博客子类rssBlog的属性? //问题:如何迭代db数据库上下文以获取rssBlog.RssFeed的所有值Entity framework 每个类的实体框架类型:C,访问不是另一个类的导航属性的子类,entity-framework,Entity Framework,有许多C/Entity框架示例可以查询和访问一个类的属性,其中该类是一个属性,而不是另一个类的子类 所以这里有一个问题,关于如何使用Entity Framework查询并访问SQL server中任何类的导航属性子类 关于如何查询和访问不是任何类的导航属性的子类,我也找不到任何示例 我只是不明白为什么微软没有这样一个基本的例子 //问题:如何使用select创建查询以访问博客子类rssBlog的属性? //问题:如何迭代db数据库上下文以获取rssBlog.RssFeed的所有值
Console.WriteLine("All blogs in the database:");
foreach (var item in query)
{
下面是Microsoft的工作示例代码,其中包含插入的问题:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using System.Diagnostics;
namespace BCodeFirstNewDatabaseSample_TPT
{
class Program
{
static void Main(string[] args)
{
try
{
using (var db = new BloggingContext())
{
// Create and save a new Blog
Console.Write("Enter a name for a new Blog: ");
var name = Console.ReadLine();
var blog = new Blog { Name = name };
db.Blogs.Add(blog);
var rssBlog = new RssEnabledBlog
{
RssFeed = "rssFeed1"
};
db.Blogs.Add(rssBlog);
db.SaveChanges();
// Display all Blogs from the database
var query = from b in db.Blogs
orderby b.Name
select b;
//每个类的实体框架类型:访问不是另一个类的导航属性的子类
//问题:如何使用select创建查询以访问博客子类rssBlog的属性
//问题:如何迭代db.Blogs以获取rssBlog.RssFeed的所有值
Console.WriteLine("All blogs in the database:");
foreach (var item in query)
{
//这仅显示如何访问博客,它是RssEnabledBlog的父级。
//我也想访问rssBlog.RssFeed。怎么做
Console.WriteLine(item.Name);
}
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
catch (Exception e)
{
Console.Write("exception {0}",e.Message);
}
}
}//program
public class Blog
{
public int BlogId { get; set; }
public string Name { get; set; }
public DateTime? Datetime { get; set; }// make DataTime nullable to avoid exception?
public virtual List<Post> Posts { get; set; }
}
public class RssEnabledBlog : Blog
{
public string RssFeed { get; set; }
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public virtual Blog Blog { get; set; }
}
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
public BloggingContext()
{//Data Source=.\SQLEXPRESS;Initial Catalog=BCodeFirstNewDatabaseSample_TPT.BloggingContext;Integrated Security=True;MultipleActiveResultSets=True;
Debug.Write(Database.Connection.ConnectionString);
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{// Thismapping creates Blog and RssEnabledBlog tables
modelBuilder.Entity<Blog>()
.Map(m => m.ToTable("Blogs"));
//base.OnModelCreating(modelBuilder);
}
}
}
这个代码有效。这里是SSMS屏幕快照:我是一个新用户。。。我还不能发布图片
谢谢。关键是你能做到
from reb in db.Blogs.OfType<RssEnabledBlog>()
EF是否使用鉴别器列来执行此操作?@MattyM,这取决于继承策略TPH、TPT、TPC。使用TPH,您将在SELECT子句中看到鉴别器。