首先,我使用EF 6.0和Code First方法.我的context配置设置为启用"代理创建"和"延迟加载".
我的问题是:延迟加载是否与返回IEnumerable(而不是IQueryable)的方法的结果一起工作?
我认为下面的代码更具说明性:
public void Test()
{
var company = GetCompanies().FirstOrDefault();
if (company.Employees.Count() > 0)
{
//I got here without errors!
}
}
public IEnumerable<Company> GetCompanies()
{
var company = context.Companies.ToList();
//Note that I did not Include the Employee (child table)
return company;
}
Run Code Online (Sandbox Code Playgroud)
请注意评论,我说:"我来到这里没有错误!".这意味着即使在ToList()通话后延迟加载也能正常工作.我认为转换后IQueryable到List或IEnumerable在EF将失去做延迟加载的能力.
我已经注意到代理仍然启用了GetCompanies方法返回的实体(在debbug模式中,我可以看到丑陋的哈希,如:System.Data.Entity.DynamicProxies.Company_7035BEA374959AC1...).
即使在不同的DLL上调用它,延迟加载也可以工作.它是否正确?我的意思是,即使我的方法返回IEnumerable(而不是IQueryable),一个不同的DLL可以在我的数据库中进行后续调用吗?
任何澄清将不胜感激.
我想知道人们对通过LINQ-to-SQL支持实体框架的决定的看法?我有一个我最初在LINQ-to-SQL中开发的应用程序.我发现它是我们应用的完美解决方案.
在尝试移植到实体框架时,我感到非常惊讶.恕我直言,甚至没有准备好迎接黄金时段.没有延迟加载,没有POCO,对继承的可怕依赖.我发现它在我的情况下基本上无法使用,而是决定坚持使用LINQ-to-SQL,直到某种程度上这个实体框架可以变得更加精致.
其他人有类似的经历吗?
我有一个文件上传功能,用户可以上传文件.我想限制用户上传某些文件类型.允许的类型是:.doc,.xlsx,.txt,.jpeg.
我怎么能这样做?
这是我的实际文件上传代码:
public ActionResult UploadFile(string AttachmentName, BugModel model)
{
BugModel bug = null;
if (Session["CaptureData"] == null)
{
bug = model;
}
else
{
bug = (BugModel)Session["CaptureData"];
}
foreach (string inputTagName in Request.Files)
{
HttpPostedFileBase file1 = Request.Files[inputTagName];
if (file1.ContentLength > 0)
{
string path = "/Content/UploadedFiles/" + Path.GetFileName(file1.FileName);
string savedFileName = Path.Combine(Server.MapPath("~" + path));
file1.SaveAs(savedFileName);
BugAttachment attachment = new BugAttachment();
attachment.FileName = "~" + path.ToString();
attachment.AttachmentName = AttachmentName;
attachment.AttachmentUrl = attachment.FileName;
bug.ListFile.Add(attachment);
model = bug;
Session["CaptureData"] …Run Code Online (Sandbox Code Playgroud) 我试图使用下面的代码允许所有用户能够修改文件夹:
class Program
{
private const string FileName = "test.txt";
private static readonly string FilePath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\TEST\\" + FileName;
private static readonly string UserName = Environment.UserDomainName;
static void Main()
{
DirectorySecurity securityRules = new DirectorySecurity();
string dirPath = Path.GetDirectoryName(FilePath);
securityRules.AddAccessRule(new FileSystemAccessRule("Users", FileSystemRights.Modify, AccessControlType.Allow));
if (dirPath == null) throw new InvalidOperationException("Failure to save local security settings");
if (!Directory.Exists(dirPath)) Directory.CreateDirectory(dirPath, securityRules);
File.WriteAllText(FilePath, "Test test!");
}
}
Run Code Online (Sandbox Code Playgroud)
运行代码后,"用户"将添加到文件夹中,但不会分配任何权限.所有的读,写,执行等都不会被选中.除了特殊权限被选中.
如何为所有用户添加包含所有修改的文件夹?
我想知道如何重定向用户.我有一个控制器索引(),我希望只有具有"学生"角色的用户才能进入那里!所以我用
[Authorize(Roles="Student")]
Run Code Online (Sandbox Code Playgroud)
我想知道如何将没有此角色的用户重定向到主页