我使用此代码生成菜单,此菜单使用此技术从数据库(类别表)填充项目
局部视图:
@using SarbarzDarb.Helper
@model IEnumerable<SarbarzDarb.Models.Entities.Category>
@ShowTree(Model)
@helper ShowTree(IEnumerable<SarbarzDarb.Models.Entities.Category> categories)
{
foreach (var item in categories)
{
<li class="@(item.ParentId == null && item.Children.Any() ? "dropdown-submenu" : "")">
@Html.ActionLink(item.Name, actionName: "Category", controllerName: "Product", routeValues: new { Id = item.Id, productName = item.Name.ToSeoUrl() }, htmlAttributes: null)
@if (item.Children.Any())
{
ShowTree(item.Children);
}
</li>
}
}
Run Code Online (Sandbox Code Playgroud)
我也是这样通过模型从控制器传递到局部视图以上:
public IList<Category> GetAll()
{
return _category.Where(category => category.ParentId == null)
.Include(category => category.Children).ToList();
}
public ActionResult Categories()
{
var query = GetAll();
return PartialView("_Categories",query);
} …Run Code Online (Sandbox Code Playgroud) asp.net-mvc recursion entity-relationship razor ef-code-first
在我的实体框架中,种子方法我有以下行来从不同的项目中获取文件:
var filePath = new DirectoryInfo(HostingEnvironment.ApplicationPhysicalPath).Parent.FullName + "\\Com.ProjectX\\companies.xls";
Run Code Online (Sandbox Code Playgroud)
这在HttpContext可用时有效,就像使用此操作方法触发它一样:
public ActionResult About()
{
var configuration = new Com.EntityModel.Configuration();
var migrator = new System.Data.Entity.Migrations.DbMigrator(configuration);
migrator.Update();
return View();
}
Run Code Online (Sandbox Code Playgroud)
但是,当我Update-Database从包管理器控制台执行时它不起作用(找不到文件并且很难调试,因为我在执行此操作时也无法断点).
我希望能够使用Update-Database命令并让它在没有HttpContext的情况下工作.我怎样才能得到这条路?
当我尝试加载父实体的子实体时,它加载默认值.如果我尝试显式加载它会抛出异常 违反Multiplicity约束."CodeFirstNamespace.Association_Customer"关系的角色"Association_Customer_Target"具有多重性1或0..1.检索复杂图形的子实体时抛出此异常.
我有一个图表关联,它有一个子实体Customer,关系为1到0或1,并且具有独立关联.*主键*是共享的.我正在使用EF6.延迟加载已启用.
public class Association
{
public virtual long Id { get; set; }
public virtual string ExternalId { get; set; }
public virtual int OrganizationId { get; set; }
public virtual AssociationType AssociationType { get; set; }
public virtual Customer Customer {get; set;}
public Association()
{
Customer = new Customer();
}
}
Run Code Online (Sandbox Code Playgroud)
客户类.
public class Customer
{
public virtual long Id { get; set; } …Run Code Online (Sandbox Code Playgroud) entity-framework ef-code-first entity-framework-ctp5 entity-framework-6
假设我们有Web应用程序的体系结构模型,其中每个帐户有1个数据库.这些帐户的数据库结构相同,仅与in中的数据不同.如何在代码优先模型中配置迁移.
entity-framework code-first ef-code-first ef-migrations entity-framework-6
我在这里找到stackoverflow上的帖子之后设置了我的DbContext模型.
这是目前的设置......
public static class DbContext
{
public static MyDbContext Db
{
get
{
if (!HttpContext.Current.Items.Contains("_db"))
{
HttpContext.Current.Items.Add("_db", new MyDbContext());
}
return HttpContext.Current.Items["_db"] as MyDbContext;
}
}
}
Run Code Online (Sandbox Code Playgroud)
上下文在end_request上的global.asax中处理,如下所示:
void Application_EndRequest(object sender, EventArgs e)
{
var db = (MyDbContext)HttpContext.Current.Items["_db"];
if (db != null)
db.Dispose();
}
Run Code Online (Sandbox Code Playgroud)
这样,在我的系统中,我可以访问Db DbContext.Db.xxxx
到目前为止,一切都在本地运行,但是,我还没有在生产环境中测试过多个用户.
我的顾虑...
我在stackoverflow上阅读了这篇文章,现在让我担心多个用户访问静态上下文可能存在数据问题.这应该关注我还是我的安装方式好吗?
我有如下模型:
public class User
{
private string password;
public string Password
{
get { return Decrypt(password); }
set { password = Encrypt(value); }
}
}
Run Code Online (Sandbox Code Playgroud)
我想从用户代码中轻松使用插入和选择密码,并以明文形式查看并希望逻辑层处理加密和解密.
不过,在选择时,EF会set的value再这样从数据库得到加密的密码将被再次加密,对我来说,从客户端代码这是一个问题得到.我也无法区分Insert和Select设置条件.
我很可能这样做:
//Insert
user.Password = Encrypt("123"); //Encrypt or Hash does not matter
context.Users.Add(user);
//Select
var hashedpassword = context.Users.Find(1).Select(u => u.Password).Single();
var unhashed = Decrypt(hashedpassword);
Run Code Online (Sandbox Code Playgroud)
相反,我希望从客户端代码中看不出来:
//Insert
user.Password = "123"; //Inserts encrypted password in database
context.Users.Add(user);
//Select
var cleartextpassword = context.Users.Find(1).Select(u => u.Password).Single();
Run Code Online (Sandbox Code Playgroud)
希望我能够解释这个问题,有人可以指出我正确的方向.如果它是否可能.
我第一次使用Entity Framework.我正在使用现有数据库的Code First开发.一切都很好但我不喜欢使用表名作为类名.数据库表没有在C#中创建友好的类名.人们重命名由EF自动生成的类是否常见?这是否会导致一些我不期待的问题?FWIW,我并不担心表格的未来架构变化.
我谷歌了几次,但似乎没有人问过这个问题.也许我应该把它当作一个标志......
我使用实体框架6.1和Code First方法.当我使用DbContext.SaveChanges()作为结果我可以得到
写入底层数据库的对象数.
但是可以获得高级统计数据:删除的行数,编辑的行数,添加的行数?
我的超级目标是获得每个实体的静态.
也许在实际调用SaveChanges()之前..
我正在使用代码优先迁移来为我的asp mvc站点创建和更新数据库.
我有一个DbContext,它位于解决方案的另一个项目中.
public class EFDbContext : DbContext
{
public DbSet<Book> Books { get; set; }
public DbSet<Author> Authors { get; set; }
public DbSet<Publisher> Publishers { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
当我启用迁移和添加迁移[名称]时.它似乎创建了自己的db
[foo-projectname].Domain.Concrete.EFDbContext
Run Code Online (Sandbox Code Playgroud)
而不是附加到我创建的数据库,称为[foo-projectname].在站点的webconfig中,连接字符串通过目录" [foo-projectname] " 引用.
如果我将webconfig更改为其他数据库,那么我将从已添加到该数据库的项目中获取结果.但是,我想使用我创建的数据库.
我不想坚持使用这个自动创建的原因是因为我不确定迁移到SqlServer并且不想再陷入困境.我也得到了错误
支持'EFDbContext'上下文的模型自...以来发生了变化
即使我没有改变任何东西.
我有一个MVC 5应用程序.我想使用标准的aspnet.identity提供程序来启用用户通过标准\ Account视图登录.但是,我还需要连接到第三方数据库,并且也希望首先使用EF代码.
我从Scott Allen Pluralsight获取的主数据库的上下文.我不知道"ApplicationUser"类为什么或继续从IdentityUser继承,但它在他的视频和我创建的以前的网站中工作.它看起来如下:
namespace ESBAMPortal.DataLayer
{
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
}
class PortalContext : IdentityDbContext<ApplicationUser>, ESBAMPortal.DataLayer.IPortalContext
{
public PortalContext()
: base("ESBamPortalConString")
{
this.Configuration.LazyLoadingEnabled = false;
}
public virtual DbSet<DomainClasses.Portal> Portal {get; set;}
}
}
Run Code Online (Sandbox Code Playgroud)
第三方数据库的上下文如下:
namespace ESBAMPortal.DataLayer
{
public class ESBContext : DbContext, ESBAMPortal.DataLayer.IESBContext
{
public ESBContext()
: base("ESBExceptionConString")
{
this.Configuration.LazyLoadingEnabled = …Run Code Online (Sandbox Code Playgroud) ef-code-first ×10
c# ×3
code-first ×2
dbcontext ×2
asp.net ×1
asp.net-mvc ×1
razor ×1
recursion ×1