我首先使用的是Entity Framework 4.3.1 Code,我的代码类似于下面的代码:
class Program
{
static void Main(string[] args)
{
// get LogEntry with id x..
}
}
public class MyContext : DbContext
{
public DbSet<Log> Logs { get; set; }
}
public class Log
{
public int LogId { get; set; }
public ICollection<LogEntry> LogEntries { get; set; }
}
public class LogEntry
{
public int LogEntryId { get; set; }
}在给定整数LogEntryId的情况下,获取LogEntry对象的最佳方法是什么?可以不通过Log.LogEntries属性直接获取实体吗?
发布于 2012-08-15 06:34:41
如果您有上下文的引用,那么
var entry = context.Set<LogEntry>().Find(entryId);发布于 2012-08-15 06:34:40
您的上下文中没有DbSet< LogEntry >有什么原因吗?
如果您这样做了,您将能够直接从上下文中加载它。
public class MyContext : DbContext
{
public DbSet<Log> Logs { get; set; }
public DbSet<LogEntry> LogEntries { get; set; }
}
var logEntry = context.LogEntries.SingleOrDefault(le => le.LogEntryId == someLogEntryId);https://stackoverflow.com/questions/11961610
复制相似问题