我正在建立一个MVC .NET网站.
由于我是一名学习首先设计数据库的老派程序员,因此我选择了数据库第一种方法.我也在使用"代码生成"来创建扩展名的文件.tt.到目前为止,一切都在发挥作用,除了一件令我烦恼的事情.
经典场景:
然后我回到我的代码和那些用于工作的东西,就像我放在模型字段顶部的特殊DisplayName标签一样被删除了.
例如,如果我有这个:
public partial class Blog
{
public Blog()
{
this.BlogComments = new HashSet<BlogComment>();
}
public int IDBlog { get; set; }
public string Title { get; set; }
[AllowHtml]
public string Content { get; set; }
public System.DateTime DateCreated { get; set; }
public string Author { get; set; }
public virtual ICollection<BlogComment> BlogComments { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
它会变成
public partial class Blog
{
public Blog()
{
this.BlogComments = …Run Code Online (Sandbox Code Playgroud) 我正在使用实体框架.我想最近插入(意味着最后十个)十行.该表User有两列:
userID
password
DateTime
Run Code Online (Sandbox Code Playgroud)
我怎样才能获得最近的十行?
我想知道什么是最佳做法.我被告知总是创建ViewModels并且从不使用核心Model类将数据传递给Views.那讲得通.让我把事情分开.但是什么是Model与ViewModel完全相同.我应该重新创建另一个类还是只使用它.
我觉得我应该重新创造.只想知道专家说的话......
c# asp.net-mvc entity-framework entity-framework-4 asp.net-mvc-viewmodel
CREATE TABLE [dbo].[ContentStatus](
[ContentStatusId] [int] NOT NULL,
[Status] [nvarchar](50) NOT NULL )
Run Code Online (Sandbox Code Playgroud)
创建:
public partial class ContentStatu
{
public ContentStatu()
{
this.Contents = new List<Content>();
}
public int ContentStatusId { get; set; }
public string Status { get; set; }
}
Run Code Online (Sandbox Code Playgroud) 我试图使用实体框架扩展使用批量更新,但我不确定如何做到这一点.
到目前为止,我有以下代码:
List<Guid> listIds = new List<Guid>();
listIds = listIds.Union(hem.ProductList.Where(x => x.CustListID == custListID).ToList().Select(y => y.OrderListID)).ToList();
Run Code Online (Sandbox Code Playgroud)
使用上面的查询它返回1000个订单列表.
所以我想要实现的目标:更新上面listIds中OrderListID的custListID
现在我正在尝试使用扩展的实体框架.
using (var db = new DBContextEntities())
{
var rowUpdates = db.ProductList.Update(x => x.OrderListID in listIds, x => new ProductList { CustListID = custListID});
}
Run Code Online (Sandbox Code Playgroud)
请告诉我如何实现这一目标.
我目前正在使用LINQ-> Entites w/Entity Framework v 4.2和T4生成的对象上下文遇到问题.我对LINQ查询在针对数据库执行之前如何转换为存储表达式相对了解,但我绝不是专家.据我所知,所使用的.NET函数在T-SQL中转换为它们的等价物,而那些没有等价物的函数(例如String.Format())将System.NotSupportedException在运行时抛出.
在我的动态LINQ查询中,我DateTime.Now.AddMinutes()用来过滤掉我的结果集:
public void ExecuteSearch()
{
var timeElapsedTresholdInMinutes = Convert.ToInt32( ConfigurationManager.AppSettings.Get("TimeElapsedThresholdInMinutes") );
var resultSet = ( from entity in _dbContext.EntityCollection
where /*non-date-related where clause stuff*/
&&
entity.DateAdded <= DateTime.Now.AddMinutes(-1 * timeElapsedThresholdInMinutes)
select entity);
/* do something with resultSet */
}
Run Code Online (Sandbox Code Playgroud)
在运行时,我得到了 System.NotSupportedException: LINQ to Entities does not recognize the method DateTime.Now.AddMinutes() method and this method cannot be translated into a store expression.
比方说,我的timeElapsedThreshold在评估后的值为-30.有谁知道为什么这不会映射到DATEADD(MINUTE,-30,GETDATE());?这里有什么我想念的吗?
当然,我可以将我的代码转换为:
public void ExecuteSearch() …Run Code Online (Sandbox Code Playgroud) 这类事总是让我烦恼,所以我想我会从聪明的人那里寻找一种"更好的方式".
我有一个查询,我想返回单个字段的值,这是一个int值.
int thivalue = (from q in context.tablename
where q.ID == id
orderby q.fieldname descending
select q).FirstOrDefault().fieldname;
Run Code Online (Sandbox Code Playgroud)
问题是查询可能没有返回结果,在这种情况下我希望thisvalue为0.
当然,如果没有结果,我会尝试访问不存在的字段.所以我的选择似乎是a)返回行(我不需要),所以我可以测试null并从那里开始,或者b)在它周围包装一个try catch并将值设置为0,其中看起来有点笨重.
我想也许DefaultIfEmpty()会帮助我,但似乎不是我想要的只是单个值.
那么这样做的正确方法是什么?当我只需要一个单独的值时,我只是顽固地想要不返回整行吗?
附录:(如果有人有兴趣)
在他的回答中,berkeleybross给了我两个明显相同的选择.但只有第二个给了我正确的结果.OrderByDescending似乎有些不对劲.使用Glimpse我查看了每个查询.
var nextSequence = db.PaneContents
.Where(q=>q.QuizPaneID == quizPaneId)
.OrderByDescending(q=>q.Sequence)
.Select (q=>q.Sequence)
.DefaultIfEmpty()
.First();
Run Code Online (Sandbox Code Playgroud)
产生了这个查询:
SELECT
CASE WHEN ([Limit1].[C1] IS NULL) THEN 0 ELSE [Limit1].[Sequence] END AS [C1]
FROM ( SELECT TOP (1)
[Project1].[Sequence] AS [Sequence],
[Project1].[C1] AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable1]
LEFT OUTER JOIN (SELECT
[Extent1].[Sequence] AS [Sequence], …Run Code Online (Sandbox Code Playgroud) 我正在使用EF4查询oracle数据库.
我有2个表POINTS(大约100 000行)COUNTRIES,每个点都有一个countryCode外键
我在存储库中有以下2个方法
public List<PointsTable> GetAll()
{
using (Entities context = new Entities())
{
List<PointsTable> theList = context.POINTS_TABLE.ToList();
return theList;
}
}
public List<PointsTable> GetAllComplete()
{
using (Entities context = new Entities())
{
List<PointsTable> theList = context.POINTS_TABLE.Include("Countries").ToList();
return theList;
}
}
Run Code Online (Sandbox Code Playgroud)
将GetAll需要5秒,但在GetAllComplete需要2分钟!
我使用AsParallel()但收益很荒谬.
我可以加快速度,或者是什么导致它变慢?
我试图遵循使用实体数据模型的本教程.
http://www.entityframeworktutorial.net/create-first-simple-EDM.aspx
它告诉我打开Model1.designer.cs文件.
我应该看到一堆C#代码,但我看到的是:
// Default code generation is disabled for model 'c:\users\dewi jones\documents\visual studio 2012\Projects\dewi_test\dewi_test\Dewi_Model.edmx'.
// To enable default code generation, change the value of the 'Code Generation Strategy' designer
// property to an alternate value. This property is available in the Properties Window when the model is
// open in the designer.
Run Code Online (Sandbox Code Playgroud)
我试图启用代码生成,但我无法弄清楚如何.我无法在项目属性中看到它:(我是Visual Basic的新手,非常感谢帮助.谢谢
我是ASP .NET MVC的新手。我尝试使用Entity Framework,但由于没有找到EF,因此无法使用。我搜索了一个解决方案,并在另一篇文章中说我必须更改版本。我做到了,但是没有成功。
Web.config:
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.1.2.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<!--<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-SportsStore.WebUI-20150201211232;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-SportsStore.WebUI-20150201211232.mdf" />-->
<!--<add name="EFDbContext" providerName="System.Data.SqlClient" connectionString="Data Source=(localDb)\v11.0;Initial Catalog=SportsStore;Integrated Security=True" />-->
<add name="EFDbContext" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-SportsStore.WebUI-20150201211232;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-SportsStore.WebUI-20150201211232.mdf" />
</connectionStrings>
Run Code Online (Sandbox Code Playgroud)
那是来自浏览器的错误。
“ /”应用程序中的服务器错误。
Could not load file or assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The located assembly's manifest definition does not match the …Run Code Online (Sandbox Code Playgroud)