我在这里完成了这个教程:http: //www.asp.net/vnext/overview/aspnet-vnext/getting-started-with-aspnet-vnext-and-visual-studio
我按照第2节中的步骤7 - "浏览器仍然打开,打开Class1.cs并将消息文本更改为"来自ClassLibrary1的新消息!".
不幸的是,在进行代码更改后,当我刷新浏览器时它与以前相同.我必须重新编译才能使更改生效.
有什么想法吗?我希望看到魔法发生!
Windows 7 64位Microsoft Visual Studio Professional 14 CTP版本14.0.21730.1 DP Microsoft .NET Framework版本4.5.50938
project.json:
{
"dependencies": {
"Helios": "0.1-alpha-build-0585",
"Microsoft.AspNet.Mvc": "0.1-alpha-build-1268",
"Microsoft.AspNet.Identity.Entity": "0.1-alpha-build-1059",
"Microsoft.AspNet.Identity.Security": "0.1-alpha-build-1059",
"Microsoft.AspNet.Security.Cookies": "0.1-alpha-build-0506",
"Microsoft.AspNet.Server.WebListener": "0.1-alpha-build-0520",
"Microsoft.AspNet.StaticFiles": "0.1-alpha-build-0443",
"Microsoft.Data.Entity": "0.1-alpha-build-0863",
"Microsoft.Data.Entity.SqlServer": "0.1-alpha-build-0863",
"Microsoft.Framework.ConfigurationModel.Json": "0.1-alpha-build-0233",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0-alpha",
"Classlibrary1": ""
},
"commands": {
/* Change the port number when you are self hosting this application */
"web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000"
},
"configurations": {
"net45": {
"dependencies": {
"System.Data": "", …
Run Code Online (Sandbox Code Playgroud) 我希望能够查询父实体并过滤子集合的内容.
例如,我有一个OrderHeaders集合.我想使用LINQ查询此集合以返回所有OrderHeaders,但我只想要包含一些相关的OrderDetail行.
我最好寻找一个解决方案,我可以在一个LINQ语句中完成所有这些.
以下控制台应用程序演示了此.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LINQ
{
class Program
{
static void Main(string[] args)
{
List<OrderHeader> orders = GetOrderHeaders();
var filteredOrders = from p in orders
where p.Detail.Where(e => e.StockCode == "STK2").Count() > 0
select p;
foreach (var order in filteredOrders)
{
Console.WriteLine("Account {0} ", order.AccountCode);
foreach (var detail in order.Detail)
{
Console.WriteLine("StockCode {0}, Quantity {1}", detail.StockCode, detail.Quantity);
}
Console.WriteLine();
}
// The above will return the following:
// Account CUST1
// …
Run Code Online (Sandbox Code Playgroud) 尝试保存我的实体时,我收到以下异常:
"AcceptChanges无法继续,因为对象的键值与ObjectStateManager中的另一个对象冲突.在调用AcceptChanges之前,请确保键值是唯一的."
我正在创建一个3层应用程序,其中数据访问层使用EF Code First,并且客户端使用WCF调用中间层.因此,在客户端上构建实体时,我无法让上下文跟踪实体状态.
在某些情况下,我发现在对象图中包含两次相同的实体.在这种情况下,当我尝试设置副本的实体状态时,它会失败.
例如,我有以下实体:Customer Country Curreny
所以我最终得到的是对象图中同一个实体的两个实例.
然后保存实体(在我的服务中)我需要告诉EF两个货币实体都没有被修改(如果我不这样做,我会得到重复).问题是我得到了上面的例外.
保存如果我将Country实例上的Currency实例设置为null,它解决了问题,但我觉得代码变得越来越混乱(由于这和其他WCF相关的EF解决方法我不得不放到位).
有没有关于如何以更好的方式解决这个问题的建议?
非常感谢您提前提供任何帮助.这是代码:
using System;
using System.Collections.Generic;
using System.Data.Entity.ModelConfiguration;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
namespace OneToManyWithDefault
{
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public Country Country { get; set; }
public Currency Currency { get; set; }
public byte[] TimeStamp { get; set; }
}
public class Country
{
public int Id { …
Run Code Online (Sandbox Code Playgroud) 任何人都可以就是否最佳做法提供单一上下文或多个上下文提出任何建议?
例如,我应该有如下单个上下文:
public class MyContext
: DbContext
{
public DbSet<Company> Companies { get; set; }
public DbSet<Country> Countries { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new CountryConfiguration());
modelBuilder.Configurations.Add(new CompanyConfiguration());
base.OnModelCreating(modelBuilder);
}
}
Run Code Online (Sandbox Code Playgroud)
或者我会更好地为公司和国家创建单独的背景?所以ConpanyContext和CountryContext都会公开一个DbSet属性.
它可能只是个人选择,但我们的数据库将包含100个实体.因此,我希望从一开始就做到这一点.
非常感谢,
保罗.
我有一个Customer实体,它引用了一组地址.这里的复杂性是我希望能够将特定地址识别为默认地址.
如果可能的话,我想在Customer表中保存默认地址的FK.这似乎比在地址表中有一列来识别默认值更优雅.
在定义这种关系方面,我在使用流畅的API方面遇到了困难.当我运行以下代码时,我得到一个异常,其中说: "保存未公开其关系的外键属性的实体时发生错误.EntityEntries属性将返回null,因为无法将单个实体标识为异常的来源通过在实体类型中公开外键属性,可以更容易地在保存时处理异常.有关详细信息,请参阅InnerException."无法确定依赖操作的有效排序.由于外键约束,模型要求或存储生成的值,可能存在依赖关系."
我创建了一个控制台应用程序来显示确切的问题.在这个测试应用程序中,我有一个Customer实体,一个Address和flient api配置.
任何帮助将非常感激:
using System;
using System.Collections.Generic;
using System.Data.Entity.ModelConfiguration;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
namespace OneToManyWithDefault
{
public class Customer
{
private ICollection<Address> m_Addresses;
public Customer()
{
Addresses = new List<Address>();
}
public int Id { get; set; }
public string CompanyName { get; set; }
public virtual ICollection<Address> Addresses
{
get
{
if (m_Addresses == null)
{
m_Addresses = new List<Address>();
}
return m_Addresses;
}
set
{
m_Addresses = value;
}
}
public Address DefaultAddress { …
Run Code Online (Sandbox Code Playgroud) 我有两节课.公司有一个县反对它:
public class Company
{
public int Id { get; set; }
public string CompanyName { get; set; }
public Country HomeCountry { get; set; }
}
public class Country
{
public int Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我试图映射到现有数据库,其中Company表包含Country记录的外键.所以我可能需要首先告诉代码外键列的名称.
下面是完整的代码示例.基于我尝试的不同的事情,它目前失败了不同的例外.到目前为止,似乎还缺乏有关此问题的有凝聚力的文档.
因此,使用Code First Fluent API如何定义外键列的名称?
测试应用:
按如下方式创建数据库:CREATE DATABASE CodeFirst; 走
Use CodeFirst
create table Companies
(
Id int identity(1,1) not null,
HomeCountryId int not null,
Name …
Run Code Online (Sandbox Code Playgroud) 我正在寻找一些关于编写一些线程安全,优化,优雅的代码的建议来执行以下操作:
我想要一个静态方法来返回一个整数序列.因此,例如,应用程序启动,线程1调用GetSequence方法并表示它想要取3,因此它得到一个由0,1,2组成的整数数组.线程2然后调用该方法并说给我4,所以它返回3,4,5,6.多个线程可以同时调用此方法.
为了说明我正在考虑的事情,这是我对此的尝试:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SequenceNumberService
{
class Program
{
static void Main(string[] args)
{
int[] numbers = NumberSequenceService.GetSequence(3);
foreach (var item in numbers)
{
Console.WriteLine(item.ToString());
}
// Writes out:
// 0
// 1
// 2
Console.ReadLine();
}
}
public static class NumberSequenceService
{
private static int m_LastNumber;
private static object m_Lock = new Object();
public static int[] GetSequence(int take)
{
int[] returnVal = new int[take];
int lastNumber;
// Increment the last audit …
Run Code Online (Sandbox Code Playgroud) 我有WPF应用程序,在按钮单击处理程序中我想要启动多个任务(其中每个任务可以执行长时间运行).
我想向用户报告所有任务是否成功,或者是否成功,单个任务的错误是什么.
虽然区分至少一个例外或者所有例外,但我看不出一个好方法.
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows;
namespace WPFTaskApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Task t1 = new Task(DoSomething);
Task t2 = new Task(DoSomethingElse);
t1.Start();
t2.Start();
// So now both tasks are running.
// I want to display one of two messages to the user:
// If both tasks completed ok, as …
Run Code Online (Sandbox Code Playgroud)