Tag: entity framework

如何使用ToString()方法将整数转换为LINQ中的字符串

当我尝试在下面的LINQ Lambda表达式中使用ToString()时,我得到一个exception,说“LINQ to Entities无法识别方法’System.String ToString()’方法,并且此方法无法转换为存储表达式。 “ query = query.Where(q => q.date.ToString().Contains(filtertext) || q.invoicenum.ToString().Contains(filtertext) || q.trans_type.ToString().Contains(filtertext) || q.charge.Contains(filtertext)); 我正在使用Linq来引诱。 使用的数据库是MySQL而不是SQL Server。 立即的帮助将受到高度赞赏。

在EF 6中找不到HasOne

我是Entity Framework的新手,我正试图找出关系。 我找到了这段代码: class MyContext : DbContext { public DbSet Posts { get; set; } public DbSet Tags { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity() .HasKey(t => new { t.PostId, t.TagId }); modelBuilder.Entity() .HasOne(pt => pt.Post) .WithMany(p => p.PostTags) .HasForeignKey(pt => pt.PostId); modelBuilder.Entity() .HasOne(pt => pt.Tag) .WithMany(t => t.PostTags) .HasForeignKey(pt => […]

在流畅的api中有一个计数器部分吗?

如果没有弄错,Fluent api中[DataType(DataType.Currency)]的DataAnnotation的计数器部分是modelBuilder.Entity.Property(i => i.Price).HasColumnType(“Currency”) 。 那么[DataType(DataType.EmaillAddress)]的计数器部分是什么? 或者是否有一个网站,其中包含一个可以在数据中进行注释的列表 – 您可以在流利的api中进行操作。 因为我想使用Fluent Api进行validation和映射。 谢谢

使用Entity Framework的Code First在保存时会抛出DbUpdateException

我正在尝试在Visual Studio C#项目中创建我的第一个数据库,并将实体添加到此数据库。 我还没有设法这样做。 在尝试时,我会在DbUpdateException调用SaveChanges()时得到DbContext 。 我想保存以下实体: public class TVSeriesReference : Reference { } TVSeriesReference除了inheritanceReference TVSeriesReference什么TVSeriesReference做: public class Reference { /// /// ID of the reference. /// public int Id { get; set; } /// /// Reference to the element in theTVDB. /// public int TheTVDBId { get; set; } /// /// Whether or not […]

Visual Studio – 禁用默认代码生成

我试图遵循使用实体数据模型的本教程。 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. 我试图启用代码生成但我无法弄清楚如何。 […]

无法在Entity Framework中捕获SqlException

我想在删除实体时捕获外键exception。 但EF只会抛出自定义exception。 我需要检查是否存在外键违规,而不通过EF“手动”检查所有关系。 try { applicationDbContext.SaveChanges(); // even though debugger shows an SqlException at first, it doesnt get caught but an DBUpdateException is thrown… return RedirectToAction(“Index”); } catch (System.Data.SqlClient.SqlException ex) { if (ex.Errors.Count > 0) { switch (ex.Errors[0].Number) { case 547: // Foreign Key violation ModelState.AddModelError(“CodeInUse”, “Country code could not be deleted, because it is […]

Linq to entities – 2个键之间的第一个字符串

我正在使用entity framework,并且遇到了缺乏对某些扩展的支持的问题。 我有2个键(都是char) fromKey和toKey ,我正在构建一个Where语句,它将搜索Stores列表,并选择Store.Name的第一个字母在fromKey和toKey之间的位置。 string[0]不起作用, Substring不起作用 – 任何人都有任何想法如何绕过这个? 子串方法的代码: public static IQueryable FindActiveByName() { var r = new ReadRepo(Local.Items.Uow.Context); Tuple range = Tuple.Create(“0”, “9”); return r.Find().Where(s => range.Item1 <= s.Name.Substring(0, 1) && s.Name.Substring(0, 1) <= range.Item2); } 给我一个错误: 运算符<=无法应用于字符串 UPDATE Anwser由nemesV提供 public static IQueryable FindActiveByName() { Tuple range = Tuple.Create(‘R’, ‘S’); var r = […]

在Aggregates EntityFramework CodeFirst中列出

我最初在聚合中使用Enum,碰巧对我来说工作正常,但现在当我将属性更改为List时,我发现数据库中没有保存或检索这些值,我认为CodeFirst会创建一个单独的List的表并将行映射到那里,但事实并非如此,这些值既不存储也不存储。 总比分: public class Trainee: Entity { public int TraineeId { get; set; } public string Name { get; set; } public int Age { get; set;} public virtual List CoursesOpted { get; set; } } 枚举: public enum CoursesTypes { PHP, Networking, }

如何在Linq到EF中使用Expression <Func >的条件?

关于这个主题已经存在一些问题(例如entity framework中的Expression.Invoke? ),但是,我无法找到适合我具体情况的答案。 我想定义一个这样的方法: public IQueryable GetCustomers(Expression<Func> condition) { return from p in ctx.Customers.AsExpandable() where condition.Compile()(p) select p; } AsExpandable方法来自LinqKit(正如之前提到的线程中所建议的那样)。 但是,当我尝试像他的方式调用我的方法时: var customers = GetCustomers(c => c.ID == 1); 它抛出InvalidCastException: 无法将类型为“System.Linq.Expressions.InstanceMethodCallExpressionN”的对象强制转换为“System.Linq.Expressions.LambdaExpression”。 我究竟做错了什么?

如何重用具有略微不同的ProcessStartInfo实例的Process实例?

我有以下代码启动robocopy作为Process 。 我还需要进行数据库查询以确定每次调用robocopy我需要复制哪些目录,因此我使用ProcessStartInfo来控制传递的参数。 internal class Program { private static void Main(string[] args) { using (var context = new MyDbContext()) { IEnumerable processInfos = GetProcessInfos(context, args[0]); foreach (ProcessStartInfo processInfo in processInfos) { // How can I reuse robocopy Process instances and // how can I dispose of them properly? Process.Start(processInfo); } } } private static IEnumerable […]