小编Bas*_*ili的帖子

实体框架中多列的唯一键约束

我正在使用Entity Framework 5.0 Code First;

public class Entity
 {
   [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
   public string EntityId { get; set;}
   public int FirstColumn  { get; set;}
   public int SecondColumn  { get; set;}
 }
Run Code Online (Sandbox Code Playgroud)

我想在两者之间进行组合,FirstColumn并且SecondColumn作为独特的组合.

例:

Id  FirstColumn  SecondColumn 
1       1              1       = OK
2       2              1       = OK
3       3              3       = OK
5       3              1       = THIS OK 
4       3              3       = GRRRRR! HERE ERROR
Run Code Online (Sandbox Code Playgroud)

反正有吗?

entity-framework constraints unique-key multiple-columns ef-code-first

222
推荐指数
8
解决办法
15万
查看次数

改变实体框架6中的数据库

我在我的代码中将EF更新为EF 6.0.2我有以下代码行:

 applicationDbContext.Database .ExecuteSqlCommand(@"ALTER DATABASE
 CURRENT SET RECOVERY FULL;");
Run Code Online (Sandbox Code Playgroud)

更新后,我收到以下错误消息:

多语句事务中不允许使用ALTER DATABASE语句.

我修复了TransctionalBehavior的问题,如下面的代码:

applicationDbContext.Database.ExecuteSqlCommand(
TransactionalBehavior.DoNotEnsureTransaction, @"ALTER DATABASE CURRENT SET RECOVERY FULL;");
Run Code Online (Sandbox Code Playgroud)

我的问题:

  • 为什么我在使用EF 6时出现此错误?
  • 我的修复是对问题的有效修复还是隐藏在此解决方案背后的魔鬼?
  • 有没有其他方法可以解决这个问题?

任何帮助将不胜感激!?

entity-framework

21
推荐指数
2
解决办法
5887
查看次数

NoSQL与实体框架核心

我需要Entity Framework Core的任何NoSQL提供程序.我可以将EF-Core版本与MongoDB/Raven或其他任何东西一起使用吗?

entity-framework-core

17
推荐指数
3
解决办法
3万
查看次数

DBCC SHRINKFILE的进展

我有一个21 Gb数据库; 其中20 Gb是文件(FileStream),我从表中删除了所有文件但是当我进行备份时,备份文件仍为21 GB.

为了解决这个问题,我成了"释放未使用空间"的理念.

所以我试图缩小我的数据库,如下所示:

USE Db;
GO
-- Truncate the log by changing the database recovery model to SIMPLE.
ALTER DATABASE Db
SET RECOVERY SIMPLE;
GO
-- Shrink the truncated log file to 1 MB.
DBCC SHRINKFILE (Db, 100);
GO
-- Reset the database recovery model.
ALTER DATABASE Db
SET RECOVERY FULL;
GO

SELECT file_id, name
FROM sys.database_files;
GO
DBCC SHRINKFILE (1, TRUNCATEONLY);
Run Code Online (Sandbox Code Playgroud)

如果我在XX分钟后对数据库进行备份,那么备份文件大小为1 Gb,这样我就可以看到已经成功清理了未使用的空间.换句话说,上面的Sql代码工作正常(XX分钟后的数据库是schrunk).


这个问题我需要等待,直到此查询(收缩操作)完成,所以我努力做到以下几点: …

c# sql-server sql-server-2012

13
推荐指数
1
解决办法
1940
查看次数

如何调用默认方法而不是具体实现

为什么默认接口方法的行为在 C# 8 中发生了变化?过去的以下代码(当默认接口方法未发布时):

interface IDefaultInterfaceMethod
{
    // By default, this method will be virtual, and the virtual keyword can be here used!
    virtual void DefaultMethod()
    {
        Console.WriteLine("I am a default method in the interface!");
    }

}

interface IOverrideDefaultInterfaceMethod : IDefaultInterfaceMethod
{
    void IDefaultInterfaceMethod.DefaultMethod()
    {
        Console.WriteLine("I am an overridden default method!");
    }
}

class AnyClass : IDefaultInterfaceMethod, IOverrideDefaultInterfaceMethod
{
}

class Program
{
    static void Main()
    {
        IDefaultInterfaceMethod anyClass = new AnyClass();
        anyClass.DefaultMethod();

        IOverrideDefaultInterfaceMethod anyClassOverridden = new AnyClass();
        anyClassOverridden.DefaultMethod();
    } …
Run Code Online (Sandbox Code Playgroud)

c# c#-8.0 default-interface-member

10
推荐指数
1
解决办法
891
查看次数

C#8中的默认接口方法

请考虑以下代码示例:

public interface IPlayer
{
  int Attack(int amount);
}

public interface IPowerPlayer: IPlayer
{
  int IPlayer.Attack(int amount)
  {
    return amount + 50;
  }
}

public interface ILimitedPlayer: IPlayer
{
  new int Attack(int amount)
  {
    return amount + 10;
  }
}

public class Player : IPowerPlayer, ILimitedPlayer
{
}
Run Code Online (Sandbox Code Playgroud)

我的问题在于代码:

IPlayer player = new Player();
Console.WriteLine(player.Attack(5)); // Output 55, --> im not sure from this output. I can compile the code but not execute it!

IPowerPlayer powerPlayer = new Player();
Console.WriteLine(powerPlayer.Attack(5)); …
Run Code Online (Sandbox Code Playgroud)

c# c#-8.0 default-interface-member

9
推荐指数
1
解决办法
703
查看次数

如何在Linq to Entities查询中调用本地方法?

我有以下代码:

public void SalesCount(string customerId)
{
  ..
  ..
  return ...;
}

var resultQuery = dataContext.Customers
.Where (c => c.Name == "Alugili")
.Where (c => SalesCount(c.CustomerId) < 100);
Run Code Online (Sandbox Code Playgroud)

当我执行resultQuery时,我得到了一个SQL异常的翻译.

我需要我可以做的地方调用SalesCount这是否有解决此问题的方法!

.net c# linq entity-framework

7
推荐指数
1
解决办法
6688
查看次数

Linq查询中的意外结果始终为+ 1

我有以下代码,我期待不同的结果.
我已经例外的是以下的结果:100,110,120,200,500

  using System;
  using System.Collections.Generic;
  using System.Linq;

  namespace ConsoleApplication11
  {
    public class Program
    {
      static void Main(string[] args)
      {
        var values = new List<int> { 100, 110, 120, 200 , 500 };

        //  In my mind the result shall be like (100,0) => 100 + 0 = 100 
        //  In my mind the result shall be like (110,0) => 110 + 0 = 110 etc. 
        var sumLinqFunction = new Func<int, int, int>((x, y) => x + y);
        var …
Run Code Online (Sandbox Code Playgroud)

c# linq

7
推荐指数
2
解决办法
401
查看次数

CQRS是CRUD的替代品吗?

CQRS和CRUD有什么不同,我可以在两种情况下使用UnitOfWork和Repository模式吗?

如果我推荐给你的那个人之间有一个复杂的关系,为什么?

CQRS模式:http ://martinfowler.com/bliki/CQRS.html
CRUD:http://en.wikipedia.org/wiki/Create,_read,_update_and_delete

任何帮助将不胜感激.

architecture entity-framework architectural-patterns sharp-architecture

6
推荐指数
2
解决办法
2320
查看次数

C#中的异步索引器

最近,我们已经移动到EF 6,我们已经开始使用EF异步命令.例如,在我的存储库中,我有以下方法:

// Gets entities asynchron in a range starting from skip.
// Take defines the maximum number of entities to be returned.
public async Task<IEnumerable<TEntity>> GetRangeAsync(int skip, int take)
{
  var entities = this.AddIncludes(this.DbContext.Set<TEntity>())
        .OrderBy(this.SortSpec.PropertyName)
        .Skip(skip)
        .Take(take)
        .ToListAsync();

  return await entities;
}
Run Code Online (Sandbox Code Playgroud)

现在我必须修改异步数据检索的UI.下面是我的UI类; 此类绑定到WPF.

public sealed class RepositoryCollectionView<TEntity, TEntityViewModel> : IList<TEntityViewModel>,
                                                                          ICollectionView,
                                                                          IEditableCollectionView,
                                                                          IComparer
...                                                       
public TEntityViewModel this[int index]
{
 get
  {
       return await this.GetItem(index).Result;
  }
  set
  {
    throw new NotSupportedException();
  }
}
...
...
...
Run Code Online (Sandbox Code Playgroud)

问题:在UI中我创建了一个名为GetItemAsync(index)的新方法,我需要从Indexer调用此方法; 当我将关键字async写入索引器时: public …

c# entity-framework-6

4
推荐指数
1
解决办法
1071
查看次数

编译器是否将已处置的对象设置为null?

我有以下代码:

public class Program
{
 private static Task task;
 private static int counter = 0;

 private static void Main(string[] args)
 {
   for (int i = 0; i < 10; i++)
   {
    if (task == null)
    {
      Console.WriteLine(++counter);
    }

     using (task = new Task(Method))
     {
       task.Start();
       task.Wait();
     }

     //  task = null;

     GC.Collect();
     GC.WaitForPendingFinalizers();
   } 

   Console.ReadKey();
 }

 public static void Method() { }
 }
Run Code Online (Sandbox Code Playgroud)

我的例外输出:1 2 3 4 5 6 ...但此方法的实际输出为1!

如果我从代码行中删除注释,Task = null;那么我将成为预期的结果.

为什么处理的任务不为空!?我想,如果对象被处理掉,那么它们可以从GC设置为null(我已经强制GC收集),换句话说GC会收集处理过的(孤儿)对象并将它们置为空?!

.net c# garbage-collection

2
推荐指数
1
解决办法
91
查看次数

静态构造函数多次调用

using System;

namespace ConsoleApplication15
{
  using System.Collections.Generic;
  using System.Threading;

  public static class Program
  {
    static void Main(string[] args)
    {
      var test1 = new Test<List<int>>();
      var t = new Thread(Tester);
      t.Start();

      var test2 = new Test<List<int>>();
      var test3 = new Test<List<int>>();
      var test4 = new Test<List<int>>();
      var test5 = new Test<List<int>>();


      test1.Do();
      test2.Do();
      test3.Do();
      test4.Do();
      test5.Do();
    }

    private static void Tester()
    {
      var test5 = new Test<IList<int>>();
      test5.Do();
    }
  }

  public class Test<T> where T : IEnumerable<int>
  {

    private static Something …
Run Code Online (Sandbox Code Playgroud)

c#

2
推荐指数
1
解决办法
1868
查看次数

出于不同原因抛出相同的异常是合法的吗?

这段代码已有10年历史,没有错误处理.代码来自一个没有解析器或扫描程序的简单脚本解释器,我正在尝试捕获解释器中的所有错误并返回带有错误消息的合适错误.

    //..
    //...
    // <exception cref = "ErrorInScriptException">Wrong number of tokens.</exception>
    // <exception cref = "ErrorInScriptException">Variable not found.</exception>
    // <exception cref = "ErrorInScriptException">Variable type is not string.</exception>
    // <param name = "splitScriptLine">Split script line to be interpreted.</param>
    private void MakeString(IList<string> splitScriptLine)
    {
        //check minimum of 3 tokens
        if (Tokens < 3)
        {
            throw CreateErrorInScriptException("IDS_Invalid_Token");
        }

        var dummy = string.Empty;

        //read strings
        for (var z = 2; z < Tokens; z++)
        {
            dummy = dummy + ReadStringToken(splitScriptLine[z]);
        }

        var variable = …
Run Code Online (Sandbox Code Playgroud)

c# exception-handling

0
推荐指数
1
解决办法
163
查看次数