小编Jeh*_*hof的帖子

ServiceStack与ASP.Net Web API

我想编写一个新的REST样式API并查看ServiceStack并且非常喜欢它.但是,我已经看到Microsoft已经发布了ASP.Net Web API项目,作为新MVC 4 beta的一部分.有没有人看过新的Web API项目?你能给出每个系统的优缺点吗?

servicestack asp.net-web-api

298
推荐指数
4
解决办法
8万
查看次数

在Pending Changes视图中双击比较文件

当我在"待定更改"视图中双击文件时,Visual Studio中是否有一种方法可以更改行为.

默认行为是,Visual Studio在代码编辑器中打开文件,但我希望它打开带有最新版本的"Compare"对话框.(与TortoiseSVN中的行为相同)

tfs visual-studio-2010

54
推荐指数
6
解决办法
5200
查看次数

DataAnnotations:递归验证整个对象图

我有一个带有DataAnnotation属性的对象图,其中对象的某些属性是本身具有验证属性的类,依此类推.

在以下场景中:

public class Employee
{
    [Required]
    public string Name { get; set; }

    [Required]
    public Address Address { get; set; }
}

public class Address
{
    [Required]
    public string Line1 { get; set; }

    public string Line2 { get; set; }

    [Required]
    public string Town { get; set; }

    [Required]
    public string PostalCode { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

如果我尝试来验证EmployeeAddress与没有价值PostalCode,那么我想(和期望)例外,但我得到没有.我是这样做的:

var employee = new Employee
{
    Name = "Neil Barnwell",
    Address = new Address …
Run Code Online (Sandbox Code Playgroud)

.net c# validation recursion data-annotations

52
推荐指数
2
解决办法
2万
查看次数

如何强制Visual Studio不添加GlobalSection(性能)部分?

我使用Visual Studio内置的分析工具,现在每次检出任何文件时,此部分会自动添加到我的解决方案文件中:

GlobalSection(Performance) = preSolution
    HasPerformanceSessions = true
EndGlobalSection
Run Code Online (Sandbox Code Playgroud)

我完全删除了所有分析报告,然后从此解决方案中删除了所有源代码控制文件的本地副本并更新到最新版本.任何结账后此部分仍在*.sln文件中.

其他未使用过prfiler的队友没有这个问题.有没有人有任何想法?

profiling projects-and-solutions visual-studio-2012

52
推荐指数
1
解决办法
1万
查看次数

如何按照约定首先删除代码中的下划线外键字段

我的项目中有多个课程(包括TPT).每个POCO具有BaseClass,其中有一个GUID(称为GlobalKey)作为主键.

首先我用来DataAnnotations创建正确的外键.但是,我在将对应的GUID与对象本身同步时遇到了问题.

现在我想只有一个虚拟导航属性,以便创建数据库中的GUID字段NamingConvention.但是字段名称总是添加一个下划线,后跟单词GlobalKey(右边).当我想删除下划线时,我不想通过流畅的API通过我的所有POCO来执行此操作:

// Remove underscore from Navigation-Field     
modelBuilder.Entity<Person>()
            .HasOptional(x => x.Address)
            .WithMany()
            .Map(a => a.MapKey("AddressGlobalKey"));
Run Code Online (Sandbox Code Playgroud)

是否有任何想法通过覆盖惯例为所有POCOS做到这一点?

提前致谢.

安德烈亚斯

entity-framework naming-conventions code-first entity-framework-6

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

为什么System.Type.GetHashCode为所有实例和类型返回相同的值?

以下代码生成46104728的输出:

using System;

namespace TestApplication
{
    internal static class Program
    {
        private static void Main()
        {
            Type type = typeof(string);
            Console.WriteLine(type.GetHashCode());
            Console.ReadLine();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但这样做:

using System;

namespace TestApplication
{
    internal static class Program
    {
        private static void Main()
        {
            Type type = typeof(Program);
            Console.WriteLine(type.GetHashCode());
            Console.ReadLine();
        }
    }
 }
Run Code Online (Sandbox Code Playgroud)

然而,在http://ideone.com上,它会为每种类型产生不同的结果.此问题现已在多个系统上重现.我现在正在使用.NET 4.0.

.net c# reflection

42
推荐指数
2
解决办法
8353
查看次数

从非抽象类派生抽象类

从非抽象类派生抽象类是否可以,或者这种方法有问题吗?

这是一个小例子:

public class Task {
  // Some Members
}

public abstract class PeriodicalTask : Task {
  // Represents a base class for task that has to be done periodicaly.
  // Some additional Members
}

public class DailyTask : PeriodicalTask {
  // Represents a Task that has to be done daily.
  // Some additional Members
}

public class WeeklyTask : PeriodicalTask {
  // Represents a Task that has to be done weekly.
  // Some additional Members
}
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,我不想让类Task抽象,因为我想直接实例化它.PeriodicalTask​​应该从Task继承功能并添加一些额外的成员,但我不想直接实例化它.只应实例化PeriodicalTask​​的派生类.

.net c# inheritance abstract

40
推荐指数
3
解决办法
9942
查看次数

将修改后的WordprocessingDocument保存到新文件中

我正在尝试打开W​​ord文档,更改一些文本,然后将更改保存到新文档.我可以使用下面的代码完成第一项,但我无法弄清楚如何将更改保存到新文档(指定路径和文件名).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using DocumentFormat.OpenXml.Packaging;
using System.IO;

namespace WordTest
{
class Program
{
    static void Main(string[] args)
    {
        string template = @"c:\data\hello.docx";
        string documentText;

        using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(template, true))
        {
            using (StreamReader reader = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
            {
                documentText = reader.ReadToEnd();
            }


            documentText = documentText.Replace("##Name##", "Paul");
            documentText = documentText.Replace("##Make##", "Samsung");

            using (StreamWriter writer = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
            {
                writer.Write(documentText);
            }
        }
      }
    }
}
Run Code Online (Sandbox Code Playgroud)

我是一个完全的初学者,请原谅基本问题!

c# office-2007 openxml openxml-sdk

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

EF 6 System.Data.Objects.ObjectContext错误

我最近从实体框架5升级到实体框架6 Alpha 2,我收到以下错误:

找不到方法:'System.Data.Objects.ObjectContext System.Data.Entity.Infrastructure.IObjectContextAdapter.get_ObjectContext()'.

我打电话时会受到打击

if (Membership.ValidateUser(model.UserName, model.Password)) {}
Run Code Online (Sandbox Code Playgroud)

这曾经工作得很好,但不确定为什么会出现这个错误.有什么建议?

c# asp.net-mvc asp.net-mvc-4 entity-framework-6 azure-sql-database

34
推荐指数
4
解决办法
6万
查看次数

枚举作为实体框架中的关键5在多对多联接中抛出错误

好吧,这有点冗长/模糊,但我在特定情况下得到一个奇怪的错误,我使用Enum作为表键并尝试查询表,同时包含多个多对多相关实体.

以下示例代码中的错误是:

The type of the key field 'DietIs' is expected to be 'MvcApplication8.Models.DietIs', but the value provided is actually of type 'System.Int32'.
Run Code Online (Sandbox Code Playgroud)

在.net 4.5 Web项目中,我有以下实体配置:

public enum DietIs {
    None,
    Kosher,
    Paleo,
    Vegetarian
}

public class Diet {

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public DietIs DietIs { get; set; }

    public string Description { get; set; }
    public virtual ICollection<Recipe> Recipes { get; set; }
    public virtual ICollection<Menu> Menus { get; set; }
}

public class Recipe {
    public int Id { …
Run Code Online (Sandbox Code Playgroud)

.net enums entity-framework .net-4.5 entity-framework-5

25
推荐指数
1
解决办法
2517
查看次数