我不能为我的生活弄清楚这一点.MSDN上的文章不清楚,似乎已经过时了.我正在使用EF 5,我正在尝试为以下内容建立单向关系.所以这就是我到目前为止所拥有的.
public sealed class Capture {
/// <summary>
/// Get and Set Capture's Unique Identifier.
/// </summary>
public int Id { get; set; }
/// <summary>
/// Get and Set Capture's Operating System.
/// </summary>
public OperatingSystem OperatingSystem { get; set; }
}
public sealed class OperatingSystem {
/// <summary>
/// Operating System's Unique Identifier.
/// </summary>
public int Id { get; set; }
}
internal sealed class EntityCaptureConfiguration : EntityTypeConfiguration<Capture> {
/// <summary>
/// Create an Entity Capture …Run Code Online (Sandbox Code Playgroud) 我正在考虑在SQL Server 2008 R2中使用XML数据类型,但也使用EF和MVC.EF5是否支持XML数据类型?如果没有,那么我将不得不选择不同的数据库设计.
非常感谢.
我想编写一个通用扩展方法,它将根据用户定义的过滤器返回一组指定的对象,其签名如下:
public static IEnumerable<T> GetObjects<T>(this ObjectSet<T> os, string fieldName, object value) where T : EntityObject {}
Run Code Online (Sandbox Code Playgroud)
当然,我们可以更改签名以处理多个过滤器,但让我们首先关注单场过滤器.
您如何编写执行此操作的Linq查询?
阻止MVC 4过度发布的最佳方法是什么?
根据MS消息来源,[Bind]属性应该是通过阻止传入的表单值进入数据库来防止过度发布的最简单方法.使用最新版本的MVC和EF,这似乎没有像预期/广告一样工作,除非我遗漏了一些重要的东西.
从Wrox Professional ASP.NET MVC 4(Jon Galloway的第7章)开始,以下类应该防止过度发布:
[Bind(Exclude="IsAdmin")]
public class User
{
public int ID { get; set; }
public string FirstName { get; set; }
public bool IsAdmin { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
但是所有[Bind]属性都会阻止表单提交值绑定到模型.然后,模型具有空白/默认值,并将其写回数据库.在这种情况下,它会确保IsAdmin = false每次使用此模型调用.SaveChanges().任何"真实"值都会被覆盖.这是一个巨大的安全故障.
替代语法 - 将[Bind]放置在Edit controller action参数中 - 完全相同:
public ActionResult Edit([Bind(Exclude = "IsAdmin")] User user)
Run Code Online (Sandbox Code Playgroud)
当调用.SaveChanges()时,所有"真实"值都会被覆盖,这与K. Scott Allen关于该主题的博客文章相矛盾:http://odetocode.com/blogs/scott/archive/2012/03/11/complete-guide-对质量分配,在-ASP净mvc.aspx
唯一的选择似乎是一系列专用的ViewModel,它们都与Automapper相连.虽然安全,但这似乎是一个巨大的头痛,尤其是:
我知道有人会回应说你永远不应该将数据模型绑定到视图,但这是默认的模板行为以及它在几乎所有文档中的显示方式.此外,MVC + EF应该让生活变得更轻松,而不是更难,并且使用AutoMapper连接的ModelView类的海洋并不是我认为更容易的.
那么有谁知道如何使[Bind]功能像宣传的一样?
c# asp.net-mvc entity-framework asp.net-mvc-4 entity-framework-5
我正在开发一个类库,用于处理使用EF的现有数据库.我想避免类库(和.exe或网站)的使用者在*.config文件中包含实体连接字符串.我希望连接字符串设置为运行时.
如何使用Database First方法设置连接字符串?没有构造函数重载接受连接字符串,当我创建一个(在一个单独的分部类中)时,我得到了"UnintentionalCodeFirstException".
我已经审查过以下链接:
我一直在寻找一些博客文章来尝试为以下要求创建一个合适的解决方案,但我似乎无法将它们拼凑在一起.希望完全有人能提供帮助.
我一直在使用Repository模式和使用Automapper的接口......这是一个精简的例子:
public class BookingRepository : IBookingRepository
{
Entities context = new Entities();
public IEnumerable<BookingDto> GetBookings
{
get { return Mapper.Map<IQueryable<Booking>, IEnumerable<BookingDto>>(context.Bookings); }
}
public BookingDto GetBookingWithProduct(Guid bookingId)
{
return Mapper.Map<BookingDto>(context.Bookings.Include(c => c.Products).SingleOrDefault(c => c.BookingId == bookingId));
}
public void Update(BookingDto bookingDto)
{
var booking = Mapper.Map<Booking>(bookingDto);
context.Entry(booking).State = EntityState.Modified;
}
public void Save()
{
context.SaveChanges();
}
public void Dispose()
{
context.Dispose();
}
}
public interface IBookingRepository : IDisposable
{
IEnumerable<BookingDto> GetBookings { get; }
BookingDto GetBooking(Guid bookingId);
void Update(BookingDto …Run Code Online (Sandbox Code Playgroud) asp.net-mvc unit-of-work repository-pattern automapper entity-framework-5
我不能为我的生活找出问题所在.每次查询执行"ToList()"时,我都会收到上面的错误.
以下是有关它的更多信息:
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
The cast to value type 'Int32' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.
</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace>
at System.Data.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal) at System.Data.Common.Internal.Materialization.Shaper.GetColumnValueWithErrorHandling[TColumn](Int32 ordinal) at lambda_method(Closure , Shaper ) at System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper) at System.Data.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext() at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at TVDataWebAPI.Controllers.ETSShowsController.GetETSShows(String title, String episodeTitle, String genre, String showTypeDescription, String directorName, String releaseYear, String seasonEpisode) in …Run Code Online (Sandbox Code Playgroud) 我的模型很简单,一个客户端可以有很多电话号码:
我在实体框架中代表了这一点
生成的客户端类如下.
public partial class Client
{
public Client()
{
this.PhoneNumbers = new HashSet<PhoneNumber>();
}
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<PhoneNumber> PhoneNumbers { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
现在我需要为"创建客户端"创建一个视图页面.此页面还应有空间输入PhoneNumbers(例如:默认情况下应该有两个文本框输入电话号码)
<fieldset>
<legend>Client</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
Run Code Online (Sandbox Code Playgroud)
作为上面的"创建视图",我们可以轻松地为"model.Name"提供空间,因为它是一个简单的属性.但是我怎么能做类似的电话号码收集.. ??
我知道我们可以用丑陋的javascript代码实现这一点,但我想知道最简单易用的方法,我们可以使用ASP.NET MVC ......?
在类库Ado.net Entity Data Model中生成了POCO类.这些都是第一次生成.但数据库的变化没有得到反映.在edmx图中右键单击并选择Update Model from Database显示新创建的表,但即使在选择要添加后也不添加表.
我尝试运行.tt(通过右键单击并运行自定义工具),但即使它没有根据最新的数据库更改重新生成Poco类.
请帮忙
.net c# entity-framework entity-framework-5 entity-framework-6
我一直在尝试使用动态添加where子句来构建linq查询.我有一个网页,其中包含一系列复选框,这些复选框被选中以对应您要搜索的字段:

到目前为止我所拥有的是以下内容:
//This calls a select from table to construct the query which the where clauses will be added to
IQueryable<AutoCompleteRestultDto> query = GetAutocompleteResults();
if (FirstName == true || AllFields == true)
{
Expression<Func<AutoCompleteRestultDto, bool>> firstNameFilter = c => terms.Any(t => c.FirstName.ToLower().Contains(t.ToLower()));
query = query.Where(firstNameFilter);
}
if (LastName == true || AllFields == true)
{
Expression<Func<AutoCompleteRestultDto, bool>> lastNameFilter = c => terms.Any(t => c.LastName.ToLower().Contains(t.ToLower()));
query = query.Where(lastNameFilter);
}
if (KnownAs == true || AllFields == true)
{
Expression<Func<AutoCompleteRestultDto, bool>> knownAsFilter …Run Code Online (Sandbox Code Playgroud)