我收到了这个ReSharper警告:在关闭时访问foreach变量.使用不同版本的编译器编译时可能会有不同的行为.
这就是我正在做的事情:
@foreach(var item in Model)
{
// Warning underlines "item".
<div>@Html.DisplayBooleanFor(modelItem => item.BooleanField)</div>
}
Run Code Online (Sandbox Code Playgroud)
我的扩展如下:
public static MvcHtmlString DisplayBooleanFor<TModel, TValue>(
this HtmlHelper<TModel> helper,
Expression<Func<TModel, TValue>> expression)
{
bool value;
try
{
var compiled = expression.Compile()(helper.ViewData.Model);
value = Convert.ToBoolean(compiled);
}
catch (Exception)
{
value = false;
}
return MvcHtmlString.Create(value ? "Yes" : "No");
}
Run Code Online (Sandbox Code Playgroud)
请注意这是按预期工作但我如何避免此警告?
我将不胜感激任何帮助.
我一直在寻找这个奇怪的错误几个小时,但没有找到任何东西.我有一个非常简单的实体:
public class Company {
public Guid Id { get; set; }
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是上下文:
public class MyDBContext : DbContext {
public DbSet<Company> Companies { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
第一次运行时,一切正常.但是,当我更改实体时(例如,我为Id添加了[Key]属性),我得到了预期的"模型已更改"或出现错误.所以,我在Global.asax application_start中输入:
Database.SetInitializer<MyDBContext>
(new DropCreateDatabaseIfModelChanges< MyDBContext >());
Run Code Online (Sandbox Code Playgroud)
这是我被卡住的地方.没有编译错误,它编译时没有错误/警告.但是,当我运行我的项目时,我收到以下错误:
描述:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.
例外细节:System.TypeLoadException: GenericArguments[0], 'MyDB.Data.MyDBContext', on 'System.Data.Entity.IDatabaseInitializer1[TContext]' violates the constraint of type parameter 'TContext'
.
请问有人有线索吗?我正在使用Entity Framework 4.1(CTP5)
假设我有这样的观点:
@model App.ViewModels.Unicorn
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div id='#unicorns'>...</div>
@section Scripts {
@Scripts.Render("~/bundles/unicorns")
}
Run Code Online (Sandbox Code Playgroud)
如果Layout
在视图中删除它会将它的布局默认为_ViewStart.cshtml
文件中指定的布局,那为什么它会抱怨Cannot resolve section 'Scripts'
?
编辑:我的布局页面上有我的部分:
@RenderSection("scripts", required: false)
Run Code Online (Sandbox Code Playgroud)
请不要注意,这是不区分大小写的,因为它一直在努力与该@section Scripts
大写的"S" 和 @RenderSection("scripts
较低的".
设想
我正在编写一个 Web 应用程序,在我的例子中是 MVC,我需要使用 get 请求的响应来更新特定容器,有时我想过滤元素并从响应中找到一个元素以放入原始容器中。
我该怎么做?
我正在做一个配置提供程序,在我的服务层我有这个:
public string GetValue(string key)
{
return _ctx.Configurations.SingleOrDefault(q => q.Key == key).Value;
}
Run Code Online (Sandbox Code Playgroud)
但是我怎么能得到它的原始类型的值,我想这样做:
public T GetValue<T>(string key)
{
return (T)(object)_ctx.Configurations.Single(q => q.Key == key).Value;
}
Run Code Online (Sandbox Code Playgroud)
正如在此指出的那样:https://stackoverflow.com/a/9420236/544283,这将是滥用泛型 ......我可以忍受.
因为我知道类型我可以在方法之外转换值,并将方法作为字符串处理,但我想避免这种情况.
我已经看过像这样的问题,甚至我发现了很多,其中任何一个都为我提出了这个问题.
我们假设我有这个代码:
public class SuperObject : IDisposable
{
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing) { }
}
Run Code Online (Sandbox Code Playgroud)
protected virtual void Dispose(bool)
上SuperObject
?因为那里真的没有什么可以处理的.public interface ICustom : IDisposable { }
Run Code Online (Sandbox Code Playgroud)
public class Custom : ICustom
{
public SuperObject Super { get; protected set; }
public Custom()
{
Super = new SuperObject();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public virtual void Dispose(bool disposing)
{
if (!disposing) return;
if (Super != null) …
Run Code Online (Sandbox Code Playgroud) 我建设使用实体框架代码首先与mvc4 Web应用程序的方式分层的应用,主要是分开的Data
,Services
和Web
.
从我的网站上我这样做:
public void Foo() {
EntityService _svc = new EntityService();
Entity = _svc.FindById(1);
}
Run Code Online (Sandbox Code Playgroud)
服务方法如下所示:
private readonly MyContext _ctx = new MyContext();
public Entity FindById(long id) {
return _ctx.Entities.SingleOrDefault(q => q.EntityId == id);
}
Run Code Online (Sandbox Code Playgroud)
问题是当我需要使用多个服务时,因为每个服务都会创建它自己的上下文.
试图解决这个问题我做了这样的事情:
public class MyContext : DbContext {
private static MyContext _ctx;
public MyContext() : base("name=myConnectionString") { }
public static MyContext GetSharedInstance() {
return GetSharedInstance(false);
}
public static MyContext GetSharedInstance(bool renew) {
if(_ctx == null || renew)
_ctx …
Run Code Online (Sandbox Code Playgroud) 我有这样的模型:
public class Entity
{
[Key, Required]
public virtual long EntityId { get; set; }
[Required]
public virtual string Name { get; set; }
public virtual long? ParentEntityId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的表:
create table Entities(
EntityId bigint not null identity(1, 1),
Name nvarchar(64) not null,
ParentEntityId bigint null
)
Run Code Online (Sandbox Code Playgroud)
ParentEntityId是EntityId的外键.
当我尝试创建一个Entity实体时,这是我得到的异常:
Invalid column name 'ParentEntity_EntityId'.
我不知道为什么EF会选择该特定列的约定,但如果我这样做:
[Column("TryPickThisName")]
public virtual int? ParentEntityId { get; set; }
Run Code Online (Sandbox Code Playgroud)
使用"TryPickThisName"列名称显示相同的错误.最后,如果我正确地写了列名或删除属性,它将显示原始错误消息.
正如问题所述,我只是想知道,因为我被问到并且我没有线索,这有什么理由吗?
可能的重复:
隐式键入;为什么只是局部变量?
c# 允许这样:
public class MyClass
{
public void Foo()
{
var q = new MyObject();
}
}
Run Code Online (Sandbox Code Playgroud)
但它不允许这样做:
public class MyClass
{
var q = new MyObject();
public void Foo()
{
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
是否有一个原因?谢谢。
将Web应用程序部署到远程主机后,我收到了这个奇怪的错误.
Uncaught TypeError: Object #<error> has no method 'endsWith'
事实:
加载的文件是:
文件编写如下:
方法是这些:
String.prototype.startsWith = function (toMatch) {
var self = this;
return self.indexOf(toMatch) == 0;
};
String.prototype.endsWith = function (toMatch) {
var self = this;
return self.toLowerCase().indexOf(toMatch.toLowerCase(),
self.length - toMatch.length) !== -1;
};
String.prototype.contains = function (toMatch) {
var self = this;
return self.indexOf(toMatch) !== -1;
};
Run Code Online (Sandbox Code Playgroud)
我不是使用javascript的高手但是当我访问原型对象时,intellisense向我显示了这个:
c# ×7
.net ×3
dispose ×2
idisposable ×2
javascript ×2
jquery ×2
resharper ×2
asp.net-mvc ×1
generics ×1
razor ×1