我在自定义数据库初始化程序中调用Seed方法时遇到问题.我正在使用EF 5.0并具有以下代码:
public static class MyDatabase
{
public static void Initialize()
{
Database.SetInitializer(new MyInitializer());
}
}
public class MyInitializer : DropCreateDatabaseAlways<MyContext>
{
protected override void Seed(MyContext context)
{
base.Seed(context);
context.Roles.Add(new Role
{
ID = 1,
Name = "User",
});
context.Roles.Add(new Role
{
ID = 2,
Name = "Admin",
});
context.SaveChanges();
}
}
Run Code Online (Sandbox Code Playgroud)
这两个类存在于MVC应用程序的单独类库中.在Global.asax,我称之为Initialize()方法:
MyDatabase.Initialize();
Run Code Online (Sandbox Code Playgroud)
数据库创建得很好,只是Seed(MyContext context)方法没有调用,没有数据放入我的数据库.
我开始使用ReSharper的,它表明,当一个方法可以进行静态的.将几百种方法转换为静态方法会在很长一段时间内增加内存占用量吗?
背景
我们有一个位于后台的应用程序,FileSystemWatcher用于监视新文件的文件夹,当出现新文件时,它会生成一个窗口.
我需要做的是为这个应用程序创建一个系统托盘图标,以便我们可以向它添加简单的上下文菜单项(能够关闭应用程序而不进入任务管理器是最大的一个).
题
所有关于如何实现系统托盘图标的搜索结果都指向如何将其添加到WPF窗口而不是应用程序本身的示例,因为我的应用程序没有主窗口并且在事件发生时生成窗口我该怎么办?实现这个?
我知道new关键字是在调用类构造函数,但是在哪个阶段我们为类分配内存?
根据我的理解,它应该对应于GCHandle.Alloc(Object)方法,但我无法找到连接.
我需要向父控件添加很多控件.
但我发现,如果我想补充ParentControl.SuspendLayout和ParentControl.ResumeLayout之前和之后我添加这些控件到父,我用秒表来测量刻度:如果我删除代码ParentControl.SuspendLayout和ParentControl.ResumeLayout,它会更快.为什么会这样?
所以SuspendLayout并且ResumeLayout不应该减少添加子控件的时间,对吗?那么,有什么好处使用SuspendLayout和ResumeLayout或者换句话说,如果我不使用SuspendLayout,并ResumeLayout而是直接添加子控件的父母,有什么不好?
我正在编写自己的基于C#的应用程序启动器,并且,当我在其中填充TreeView并启动应用程序快捷方式时,我似乎无法弄清楚如何将图标作为图像添加到TreeView.我目前获取文件的代码是:
private void homeMenu_Load(object sender, EventArgs e)
{
this.ShowInTaskbar = false;
if (Directory.Exists((Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).FullName + "\\Roaming\\Launcher")))
{
}
else
{
Directory.CreateDirectory(Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).FullName + "\\Roaming\\Launcher");
}
DirectoryInfo launcherFiles = new DirectoryInfo(Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).FullName + "\\Roaming\\Launcher");
lstPrograms.Nodes.Add(CreatingDirectoryTreeNode(launcherFiles));
lstPrograms.Sort();
}
private static TreeNode CreatingDirectoryTreeNode(DirectoryInfo directoryInfo)
{
var directoryNode = new TreeNode(directoryInfo.Name);
foreach (var directory in directoryInfo.GetDirectories())
{
directoryNode.Nodes.Add(CreatingDirectoryTreeNode(directory));
}
foreach (var file in directoryInfo.GetFiles())
{
directoryNode.Nodes.Add(new TreeNode(file.Name));
}
return directoryNode;
}
Run Code Online (Sandbox Code Playgroud)
我遇到的主要问题是将TreeList的ImageList图标添加到特定节点.我知道我需要添加:
lstPrograms.ImageList.Images.Add(Icon.ExtractAssociatedIcon());
Run Code Online (Sandbox Code Playgroud)
要将图标实际添加到图像列表中,如何获取该特定图像的索引,然后将其添加到TreeView其相关文件中?
我正在尝试创建一个通用散列alogrithim,它将字符串散列为64位int.
我能够正确地散列字符串:sql:
select
convert
(
varchar(64),
HASHBYTES
(
'SHA1',
'google.com'
),
2
)
Run Code Online (Sandbox Code Playgroud)
回报 BAEA954B95731C68AE6E45BD1E252EB4560CDC45
C#
System.Security.Cryptography.SHA1 c = System.Security.Cryptography.SHA1.Create();
System.Text.StringBuilder sb = new StringBuilder();
byte[] b = c.ComputeHash(Encoding.UTF8.GetBytes("google.com"));
for (int i = 0; i < b.Length;i++ )
{
byte by = b[i];
sb.Append(by.ToString("x2").ToUpper());
}
return sb.ToString();
Run Code Online (Sandbox Code Playgroud)
retruns BAEA954B95731C68AE6E45BD1E252EB4560CDC45
但是,当我转换为bigint/long时,值不匹配:sql:
select
convert
(
bigint,
HASHBYTES
(
'SHA1',
'google.com'
)
)
Run Code Online (Sandbox Code Playgroud)
回报 2172193747348806725
C#:
System.Security.Cryptography.SHA1 c = System.Security.Cryptography.SHA1.Create();
byte[] b = c.ComputeHash(Encoding.UTF8.GetBytes("google.com"));
return BitConverter.ToInt64(b, 0);
Run Code Online (Sandbox Code Playgroud)
回报 7501998164347841210
有关如何使这些数字匹配的任何想法?
当我尝试将ASP.NET数据绑定GridView到IEnumerable<ExpandoObject>使用时ObjectDataSource,我得到以下异常.
System.Web.HttpException(0x80004005):DataBinding:'System.Dynamic.ExpandoObject'不包含名为'StoreID'的属性.
有谁知道我怎么能数据绑定到ExpandoObjects?
我正在尝试创建一个SQL函数,用于测试参数是以某个术语开头还是包含该术语,但不是以它开头.
基本上,如果参数以term开头,则函数返回0.否则返回1.
这是我所拥有的函数的骨骼,我正在尝试从我发现的另一个函数中进行调整:
CREATE FUNCTION [dbo].[fnGetRelevance]
(
@fieldName nvarchar(50),
@searchTerm nvarchar(50)
)
RETURNS @value int -- this is showing an error, it seems to expect table but all I want is an int
(
-- does this need to be here? If so, what should it be?
)
AS
BEGIN
declare @field TABLE(Data nvarchar(50))
insert into @field
select Data from @fieldName
if (Data like @searchTerm + '%') -- starts with
begin
return 0
end
else if (Data like '%' + …Run Code Online (Sandbox Code Playgroud) 我想知道有什么区别,何时使用Statement,PreparedStatement和CallableStatement.
使用这些方法的最佳实践和典型方案是什么?
c# ×7
.net ×2
sql ×2
sql-server ×2
winforms ×2
allocation ×1
asp.net-mvc ×1
bigint ×1
controls ×1
dynamic ×1
expando ×1
hash ×1
java ×1
jdbc ×1
layout ×1
memory ×1
mysql ×1
postgresql ×1
sql-function ×1
suspend ×1
system-tray ×1
t-sql ×1
treeview ×1
uint64 ×1
wpf ×1