本文将介绍使用ASP.NETMVC3和实体框架4.1代码首先在SQLCE4.0中存储图像时出错的详细情况,。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于AS
本文将介绍使用ASP.NET MVC 3和实体框架4.1代码首先在SQL CE 4.0中存储图像时出错的详细情况,。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于ASP.NET MVC2与实体框架4 – AsEnumerable()或ToList()在存储库?、asp.net – 向实体框架添加其他属性4代码首先是CTP 5实体、asp.net-mvc – ASP.NET MVC6中的实体框架7多个外键到同一个表、asp.net-mvc – ASP.NET MVC实体框架关系绑定的知识。
本文目录一览:- 使用ASP.NET MVC 3和实体框架4.1代码首先在SQL CE 4.0中存储图像时出错
- ASP.NET MVC2与实体框架4 – AsEnumerable()或ToList()在存储库?
- asp.net – 向实体框架添加其他属性4代码首先是CTP 5实体
- asp.net-mvc – ASP.NET MVC6中的实体框架7多个外键到同一个表
- asp.net-mvc – ASP.NET MVC实体框架关系绑定
使用ASP.NET MVC 3和实体框架4.1代码首先在SQL CE 4.0中存储图像时出错
我在我的学生模型中声明为:
[Column(TypeName = "image")] public byte[] Photo { get; set; }
数据库创建的照片列的图像数据类型可以在这里看到:
问题是:
当我运行应用程序,并尝试保存一张3 MB照片的学生(例如),我得到一个例外:
validationError.ErrorMessage = "The field Photo must be a string or array type with a maximum length of '4000'."
sql Server CE支持这些Data Types.在sql Express和sql Compact Edition(CE)之间的这个comparison中,我们通过使用图像数据类型支持sql CE支持二进制(BLOB)存储。
Image = Variable-length binary data
with a maximum length of 2^30–1
(1,073,741,823) bytes. Storage is the
length of the value in bytes.
形象应该做我想的工作。
我在这里做错了什么?这是一个bug吗?
注意:
我也尝试过MaxLength数据注释:
[Column(TypeName = "image")] [MaxLength(int.MaxValue)] public byte[] Photo { get; set; }
但我得到这个例外:
Binary column with MaxLength greater than 8000 is not supported.
编辑:
我发现post发布了EF 4.1。它有以下:
Change of default length for non-key
string and binary columns from ‘128’
to ‘Max’. sql Compact does not support
‘Max’ columns,when running against
sql Compact an additional Code First
convention will set a default length
of 4000. There are more details about
the change included in a recent blog
post (link below).
好吧,我唯一可以让它工作的方式是做所描述的here,也就是设置DbContext.Configuration.ValidateOnSaveEnabled = false。这是一个解决方法,因为帖子建议。
解决方法
public StudentContext() { // required to prevent bug - http://stackoverflow.com/questions/5737733 this.Configuration.ValidateOnSaveEnabled = false; }
已经找到了更好的解决方案。不要禁用验证。
[博客文章更新]
更新:LINQ to sql和EF Code First fame的@damienGuard指出,一个更好和更多的提供商不可知的解决方案是使用MaxLength而不是TypeName =“ntext”。
更新2:使用[MaxLength]可防止任何验证错误,并且不需要禁用验证。
ASP.NET MVC2与实体框架4 – AsEnumerable()或ToList()在存储库?
然而,在最近的一个问题上,我在我的代码示例中包含了一堆ToList(),并且吓倒了一些让我放心的IEnumerable更好地返回的人.
我现在很困惑,至少说.
我应该在我的存储库中返回IEnumerable,然后在我的视图模型中将它们转换为List?我应该像我以前一样在我的存储库中直接使用ToList()我假设首先要启用延迟执行吗?
吉米妮圣诞节
编辑:
所以,因为我禁用了延迟加载,基于早期的建议,我应该重新启用它,从我的存储库返回IEnumerable / IQueryable,并将视图模型中的集合转换为列表,如果需要的话?
下面的一个答案将IEnumerable与热切的执行相关联,而我的印象是只有ToList()才会立即执行查询.
我偶然发现了this,this和this,这些都包含了与这个问题有关的一些有趣的讨论.
解决方法
>你想控制提供给消费者的输出集(即你不希望它们在它上面运行查询),而且
>你不介意急于执行.
通过AsEnumerable()返回IQueryable或IEnumerable在您的存储库中,如果:
>你不介意你的消费者在输出集上运行查询
>你希望延期执行.
也可以看看
http://thinkbeforecoding.com/post/2009/01/19/Repositories-and-IQueryable-the-paging-case
asp.net – 向实体框架添加其他属性4代码首先是CTP 5实体
我有一个新闻类,它定义如下:
public class News : Entity { public int NewsId { get; set; } public string Title { get; set; } public string Body { get; set; } public bool Active { get; set; } }
我的数据库上下文类:
public class MyContext : DbContext { public DbSet<News> Newses { get; set; } }
在实体类中,我有一个定义为的属性:
public IList<RuleViolation> RuleViolations { get; set; }
我还没有对这部分进行编码,但我希望在验证对象时将所有破坏的规则添加到此列表中.我得到的错误是:
One or more validation errors were detected during model generation: System.Data.Edm.EdmEntityType: : EntityType 'RuleViolation' has no key defined. Define the key for this EntityType. System.Data.Edm.EdmEntitySet: EntityType: The EntitySet RuleViolations is based on type RuleViolation that has no keys defined.
这是我的重叠代码:
public News FindById(int newsId) { return context.Database.sqlQuery<News>("News_FindById @NewsId",new sqlParameter("NewsId",newsId)).FirstOrDefault(); }
更新2011-03-02:
这是我的实体类:
public class Entity { public IList<RuleViolation> RuleViolations { get; set; } public bool Validate() { // Still needs to be coded bool isValid = true; return isValid; } }
这是我的RuleViolation类:
public class RuleViolation { public RuleViolation(string parameterName,string errorMessage) { ParameterName = parameterName; ErrorMessage = errorMessage; } public string ParameterName { get; set; } public string ErrorMessage { get; set; } }
这是我的上下文类:
public class MyContext : DbContext { public DbSet<News> Newses { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<News>().Ignore(n => n.RuleViolations); } }
解决方法
public class MyContext : DbContext { public DbSet<News> Newses { get; set; } protected override void OnModelCreating(ModelBuilder builder) { builder.Ignore<RuleViolation>() } }
或者,您可以使用NotMapped属性忽略该属性
public class Enitity { [NotMapped] public IList<RuleViolation> RuleViolations { get; set; } //other properties here }
然后实体框架将忽略该属性.
asp.net-mvc – ASP.NET MVC6中的实体框架7多个外键到同一个表
public class Match { [Key] public int MatchId { get; set; } public DateTime playday { get; set; } public float HomePoints { get; set; } public float GuestPoints { get; set; } public int HomeTeamId { get; set; } public int GuestTeamId { get; set; } [ForeignKey("HomeTeamId")] [InverseProperty("HomeMatches")] public virtual Team HomeTeam { get; set; } [ForeignKey("GuestTeamId")] [InverseProperty("AwayMatches")] public virtual Team GuestTeam { get; set; } } public class Team { public int TeamId { get; set; } public String name { get; set; } public virtual ICollection<Match> HomeMatches { get; set; } public virtual ICollection<Match> AwayMatches { get; set; } }
这是我找到的最好的方法,因为我可以添加一个新的迁移,一切都没问题,但是当我更新数据库时,我得到一个像这样的错误
Introducing FOREIGN KEY constraint ‘FK_Match_Team_HomeTeamId’ on table ‘Match’ may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION,or modify other FOREIGN KEY constraints.
Could not create constraint or index. See prevIoUs errors.
解决方法
由于Match类中的两个属性存在问题
public int HomeTeamId { get; set; } public int GuestTeamId { get; set; }
以及将生成的外键HomeTeamId和GuestTeamId. EF7使用ON DELETE CASCADE生成外键,不能将其用作一个外键.实体框架(RC1)的当前实现没有注释属性,您可以使用它来更改行为.
该问题的第一个解决方案是使用可空的属性,如
public int? HomeTeamId { get; set; } public int? GuestTeamId { get; set; }
要么
public int HomeTeamId { get; set; } public int? GuestTeamId { get; set; }
最多一个属性应该是不可为空的.结果问题将得到解决,但是会有一个小缺点,这对某些情况可能并不重要. nullable属性的数据库表中的字段在列定义中没有NOT NULL属性.
如果确实需要同时保持HomeTeamId和GuestTeamId不可为空,那么您可以通过修改上下文类(继承自DbContext)来解决问题,其中使用Match和Team类.
您已经在下面定义了一些上下文类
public class MyDBContext : DbContext { DbSet<Team> Teams { get; set; } DbSet<Match> Matches { get; set; } }
要解决描述问题,可以在显式设置的类中添加受保护的OnModelCreating
public class MyDBContext : DbContext { protected override void OnModelCreating(ModelBuilder modelbuilder) { base.OnModelCreating(modelbuilder); modelbuilder.Entity(typeof (Match)) .HasOne(typeof (Team),"GuestTeam") .WithMany() .HasForeignKey("GuestTeamId") .OnDelete(DeleteBehavior.Restrict); // no ON DELETE modelbuilder.Entity(typeof (Match)) .HasOne(typeof (Team),"HomeTeam") .WithMany() .HasForeignKey("GuestTeamId") .OnDelete(DeleteBehavior.Cascade); // set ON DELETE CASCADE } DbSet<Team> Teams { get; set; } DbSet<Match> Matches { get; set; } }
您可以在两个外键上使用原因DeleteBehavior.Restrict(而不是在一个外键上使用DeleteBehavior.Cascade).重要的是要注意,最后一种方法允许将HomeTeamId和GuestTeamId(如数据库中的相应字段)保持为不可为空.
有关其他信息,请参见the documentation.
asp.net-mvc – ASP.NET MVC实体框架关系绑定
例;
我有User类和关系部门
当我在控制器中使用它时
using(context) { var user = context.Find(id); string department = user.Department.Name; }
它在上下文中调用时工作.但是当我这样做的时候
using(context) { var user = context.Find(id); return View(user); }
并在视图中调用
Model.Department.Name
我收到了错误.
这是我的答案但不好
using(context) { var user = context.Find(id); string department = user.Department.Name; return View(user); }
当我尝试在视图中使用Model.Department.Name时,我没有错误,当我使用类作为模型时,我必须为每个关系做到这一点.这个问题有更好的解决方案吗?我希望在视图中使用所有关系而不在控制器中调用它们.
我希望你能理解我,对不起我的英语.
解决方法
context.Users.Include(u => u.Department).FirstOrDefault(u => u.Id == id);
或者如果您使用的是旧版本的实体框架,则此方法的通用版本可能不可用:
context.Users.Include("Department").FirstOrDefault(u => u.Id == id);
今天的关于使用ASP.NET MVC 3和实体框架4.1代码首先在SQL CE 4.0中存储图像时出错的分享已经结束,谢谢您的关注,如果想了解更多关于ASP.NET MVC2与实体框架4 – AsEnumerable()或ToList()在存储库?、asp.net – 向实体框架添加其他属性4代码首先是CTP 5实体、asp.net-mvc – ASP.NET MVC6中的实体框架7多个外键到同一个表、asp.net-mvc – ASP.NET MVC实体框架关系绑定的相关知识,请在本站进行查询。
本文标签: