我正在使用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
我在我的代码中将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)
我的问题:
任何帮助将不胜感激!?
我需要Entity Framework Core的任何NoSQL提供程序.我可以将EF-Core版本与MongoDB/Raven或其他任何东西一起使用吗?
我有一个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# 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) 请考虑以下代码示例:
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) 我有以下代码:
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这是否有解决此问题的方法!
我有以下代码,我期待不同的结果.
我已经例外的是以下的结果: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) 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
最近,我们已经移动到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 …
我有以下代码:
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会收集处理过的(孤儿)对象并将它们置为空?!
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) 这段代码已有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)