在我的开发机器上,我尝试使用包管理器控制台中的update-database重新创建我的数据库.我相信我已按照上述帖子中的说明进行操作.
我收到此错误消息:
发生文件激活错误.物理文件名'\ WRDatabase.mdf'可能不正确.诊断并更正其他错误,然后重试该操作.CREATE DATABASE失败.无法创建列出的某些文件名.检查相关错误.
我正在运行的命令是:
update-database -ConfigurationTypeName WRConfiguration -ConnectionStringName localWR -Verbose
Run Code Online (Sandbox Code Playgroud)
请注意,在上面的命令中,我传递了一个连接字符串名称,并且看起来正在使用连接字符串,因为.mdf文件的名称出现在错误消息中.
有趣的是,我能够运行我的代码并且我的数据库已经创建,但它是用错误的名称创建的(它被命名为"VIN.DataModel.WRContext",它是DbContext的完整命名空间和类名).我的猜测是,当我的代码运行时,EF无法找到用于创建数据库的连接字符串.这是故意的,因为我将此上下文映射到在服务器上运行的数据库以及在本地计算机上运行的db的副本(即,我将为同一个DbContext使用两个连接字符串).
这是应用程序配置:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
<connectionStrings>
<add name="localWR" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog =WRDatabase;AttachDBFilename=|DataDirectory|\WRDatabase.mdf; Integrated Security=True;MultipleActiveResultSets=True" />
</connectionStrings>
Run Code Online (Sandbox Code Playgroud)
在Configuration类的构造函数中,AutomaticMigrationsEnabled设置为false.
这是上下文类:
public class WRContext : DbContext
{
static WRContext()
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<WRContext, VIN.DataModel.WRMigrations.WRConfiguration>());
}
public WRContext()
{
}
public WRContext(string connectionString)
: base(connectionString)
{
}
public DbSet<Emp> Emps { get; set; }
public DbSet<Inv> Invs { get; set; } …Run Code Online (Sandbox Code Playgroud) 1.)从.net客户端,我如何测试客户端是否连接到服务器(即可以发送和接收)是的,我可以在try块内发送消息并捕获随后的异常,但我希望更优雅的解决方案.
2)如何打开,关闭和重新打开连接?在我尝试解决上面的问题1时,我发现如果我打开连接然后调用connection.Close()我无法从连接工厂获得另一个连接(请参阅下面的代码片段).我收到错误消息XMSCC0008
我使用的是非常标准的vanilla MQ配置.以下是我的客户端连接方式:
ISession session = MQAccess.GetSession(MQAccess.Connection);
IDestination destination = session.CreateTopic(SubTopicName);
Consumer = MQAccess.GetConsumer(session, destination);
Consumer.MessageListener = new MessageListener(HandleMQSubEvent);
MQAccess.Connection.Start();
Run Code Online (Sandbox Code Playgroud)
其中MQAccess是一个小实用程序类.
编辑了添加MQAccess代码的问题:
public static class MQAccess
{
public static readonly MQConfigurationSectionHandler ConfigSettings;
public static readonly IConnectionFactory ConnectionFactory;
private static readonly IConnection connection;
public static IConnection Connection
{
get { return connection; }
}
static MQAccess()
{
ConfigSettings = (MQConfigurationSectionHandler)
ConfigurationManager.GetSection("mq-configuration");
XMSFactoryFactory factory = XMSFactoryFactory.GetInstance(XMSC.CT_WMQ);
ConnectionFactory = factory.CreateConnectionFactory();
ConnectionFactory.SetStringProperty(XMSC.WMQ_HOST_NAME, ConfigSettings.Hostname);
ConnectionFactory.SetIntProperty(XMSC.WMQ_PORT, ConfigSettings.Port);
ConnectionFactory.SetStringProperty(XMSC.WMQ_CHANNEL, ConfigSettings.Channel);
if (ConfigSettings.QueueManager == string.Empty)
{
ConnectionFactory.SetStringProperty(XMSC.WMQ_QUEUE_MANAGER, …Run Code Online (Sandbox Code Playgroud) 在MainWindow中,命令绑定工作正常.在UserControl1中它不起作用.请注意,datacontext设置正确,因为按钮的内容是绑定的结果.
我不是试图将usercontrol中的命令绑定到mainwindow中的命令或任何其他此类欺骗.我只是想在UserControl1中复制我在MainWindow中所做的事情.
MainWindow XAML
<StackPanel>
<Button Content="Click Here" Command="{Binding ClickHereCommand}" Height="25" Width="90"></Button>
<local:UserControl1></local:UserControl1>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
MainWindow代码背后
public partial class MainWindow : Window
{
public static RoutedCommand ClickHereCommand { get; set; }
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
ClickHereCommand = new RoutedCommand();
CommandBindings.Add(new CommandBinding(ClickHereCommand, ClickHereExecuted));
}
public void ClickHereExecuted(object sender, ExecutedRoutedEventArgs e)
{
System.Windows.MessageBox.Show("hello");
}
}
Run Code Online (Sandbox Code Playgroud)
UserControl XAML
<UserControl x:Class="CommandBindingTest.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" x:Name="root">
<Grid DataContext="{Binding ElementName=root}" >
<Button Content="{Binding ButtonContent}" Command="{Binding ClickHereCommand}" Height="25" Width="90"></Button>
</Grid>
</UserControl> …Run Code Online (Sandbox Code Playgroud) http://msdn.microsoft.com/en-us/library/dd997415.aspx
根据上面引用的文章,我试图处理continuatin任务中的异常.我在上面的文章中引用的例子是这样的:
var task1 = Task.Factory.StartNew(() =>
{
throw new MyCustomException("Task1 faulted.");
})
.ContinueWith((t) =>
{
Console.WriteLine("I have observed a {0}",
t.Exception.InnerException.GetType().Name);
},
TaskContinuationOptions.OnlyOnFaulted);
Run Code Online (Sandbox Code Playgroud)
我的代码是:
Task<string> task = Task<string>.Factory.StartNew(() => process.StartTask(this));
task.ContinueWith(CloseDialog, TaskContinuationOptions.OnlyOnFaulted);
Run Code Online (Sandbox Code Playgroud)
在StartTask中,我像示例一样抛出错误.我的期望是CloseDialog将执行,我可以检查该方法中的task.Exception,如示例所示.但是,当我抛出异常时,代码只会因未处理的异常而停止.我应该使用try/catch块吗?如果是的话,在哪里?顺便说一句,我希望我的延续任务(CloseDialog)始终运行.我只是使用.OnlyOnFaulted,因为这是示例中显示的内容.
我想做这个:
NHibernate.IQueryOver<DataAccess.Domain.Product, DataAccess.Domain.Product> query = session.QueryOver<DataAccess.Domain.Product>();
query = query.Where(x => x.Name == "X");
query = query.Take(1).Skip(3);
List<Product> results = query.List().ToList();
Run Code Online (Sandbox Code Playgroud)
我无法在Skip或Take上找到任何帮助.工具提示帮助(是的,我是绝望的)说Skip和Take返回IQueryOver,但是错误消息说明了"无法隐式地将IQueryOver {T}转换为IQueryOver {T,T}."我不知道IQueryOver是什么{T,T}是.无论如何,我没有要求其中一个.
我需要将其中一个表上主键的数据类型从 int 更改为字符串(我没有说我想要...)。迁移生成以下代码:
AlterColumn("dbo.Venues", "ID", c => c.String(nullable: false, maxLength: 128));
Run Code Online (Sandbox Code Playgroud)
这会导致 SQL Server 抱怨数据类型必须是 int、bigint 等,因为该列是标识列。我添加了以下内容,但似乎没有效果:
AlterColumn("dbo.Venues", "ID", c => c.Int(identity:false));
Run Code Online (Sandbox Code Playgroud)
问题:如何进行迁移以将数据类型从 int 更改为 string?
以下可能与我的问题有关,但不一定:迁移生成的唯一代码是上面显示的第一个 AlterColumn。当以本机生成方式运行时,迁移导致 SQL Server 抱怨它无法更改列,因为存在依赖关系。事实上,唯一的依赖是它是一个 PK(没有表将它引用为 FK)。我将迁移修改为如下所示,现在我收到了如上所示的数据类型错误。
DropPrimaryKey("dbo.Venues");
AlterColumn("dbo.Venues", "ID", c => c.Int(identity:false));
AlterColumn("dbo.Venues", "ID", c => c.String(nullable: false, maxLength: 128));
AddPrimaryKey("dbo.Venues", "ID");
Run Code Online (Sandbox Code Playgroud) 以下是使用它的上下文:
long dirtyFlag = 0x0000;
if (erd.SupervisorCompType != orgErd.SupervisorCompType) // change has been made?
{
dirtyFlag |= 0x0001;
// etc...
}
Run Code Online (Sandbox Code Playgroud) c# ×4
.net ×2
c#-4.0 ×1
entity-framework-migrations ×1
ibm-mq ×1
mq ×1
nhibernate ×1
queryover ×1
websphere ×1
wpf ×1