小编And*_*ler的帖子

SQL:在保留约束的同时规范化数据库

假设我有以下表格:

     ____________________             ____________________
    |     Organisms      |           |       Species      |
    |--------------------|           |--------------------|
    |OrganismId (int, PK)|           |SpeciesId (int, PK) |
    |SpeciesId (int, FK) |?---------1|Name (varchar)      |
    |Name (varchar)      |           |____________________|
    |____________________|                      1
              1                                 |
              |                                 |
              |                                 |
              ?                                 ?
    ______________________        ____________________          _______________
   | OrganismPropsValues  |      |   SpeciesProps     |        |     Props     |
   |----------------------|      |--------------------|        |---------------|
   |OrganismId (int, FK)  |      |PropId (int,PK,FK)  | ?-----1|PropId (int,PK)|
   |PropId (int, FK)      |      |SpeciesId(int,PK,FK)|        |Name (varchar) |
   |Value (varchar)       |      |____________________|        |_______________|
   |______________________|                                             1
              ?                                                         |
              | …
Run Code Online (Sandbox Code Playgroud)

sql polymorphism database-design sql-server-2008 entity-attribute-value

5
推荐指数
0
解决办法
387
查看次数

用JSON(C#)序列化枚举值

可能重复:
c#enum的JSON序列化为字符串

我有两个课程如下:

Transaction
    int OrderNumber
    // ... snip ...
    IEnumerable<Item> Items

Item
    string Sku
    // ... snip ...
    ItemCategory Category
Run Code Online (Sandbox Code Playgroud)

ItemCategory是一个如下所示的枚举:

[DataContract]
public enum ItemCategory
{
    [EnumMember(Value = "Category1")]
    Category1,

    [EnumMember(Value = "Category2")]
    Category2
}
Run Code Online (Sandbox Code Playgroud)

我的两个类适当地使用DataContract和DataMember属性进行修饰.

我正在尝试获取Transaction项的JSON表示.在Transaction中,我有一个如下所示的公共方法:

public string GetJsonRepresentation()
{
    string jsonRepresentation = string.Empty;

    DataContractJsonSerializer serializer = new DataContractJsonSerializer(this.GetType());
    using (MemoryStream memoryStream = new MemoryStream())
    {
        serializer.WriteObject(memoryStream, this);
        jsonRepresentation = Encoding.Default.GetString(memoryStream.ToArray());   
    }

    return jsonRepresentation;
}
Run Code Online (Sandbox Code Playgroud)

这将返回一个如下所示的字符串:

{
    "OrderNumber":123,
    "Items":[{"SKU": "SKU1","Category": 0}]
}
Run Code Online (Sandbox Code Playgroud)

这是我想要的,除了每个Item的"Category"枚举值被序列化为其整数值,而不是我在EnumMember属性中指定的值.我如何得到它,以便返回的JSON看起来像"类别":"Category1"而不是"Category":0?

c# serialization json

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

多次处理对象

我有以下代码,它使用流打开和修改 Open XML 文档,然后保存该流的新二进制表示:

MemoryStream stream = null;
try
{
    stream = new MemoryStream();
    stream.Write(this.GetBinaryRepresentation(), 0, this.GetBinaryRepresentation().Length);

    using (WordprocessingDocument document = WordprocessingDocument.Open(stream, true))
    {
        OfficeDocument.ModifyDocument(document);
        this.SetBinaryRepresentation(stream.ToArray());
        stream = null;
    }
}
finally
{
    if (stream != null)
    {
        stream.Dispose();
    }
}
Run Code Online (Sandbox Code Playgroud)

我最初使用了两个 using 块(一个用于 MemoryStream,第二个用于 WordprocessingDocument),但收到警告 CA2202:“对象‘流’可以在方法中多次处理......”根据MSDN 文章,我修改了代码到上面(将外部使用转换为尝试),但我仍然收到此警告。

我不确定如何构造此方法以确保在流上只调用一次 Dispose。我不想简单地取消此警告,因为 MSDN 文章指出您不应该依赖 Dispose 可以多次安全调用。

c# ca2202

4
推荐指数
1
解决办法
4159
查看次数

C#语法糖类似于SQL的IN比较?

我经常发现自己写的条件类似于以下内容:

if(Path.GetExtension(filename) == ".pdf" || Path.GetExtension(filename)== ".doc")
{
    // do something
}
Run Code Online (Sandbox Code Playgroud)

每个我想测试的文件扩展名调用Path.GetExtension()一次似乎有点多余.当然,我可以这样做:

string fileExtension = Path.GetExtension(filename);
if(fileExtension == ".pdf" || fileExtension == ".doc")
{
    // do something
}
Run Code Online (Sandbox Code Playgroud)

但考虑到我只使用fileExtension进行比较而没有别的,声明文件扩展名的变量似乎并不优雅.

在SQL中,我可以使用IN运算符:

SELECT file FROM table WHERE fileExtension IN(".pdf", ".doc")
Run Code Online (Sandbox Code Playgroud)

这让我可以毫不重复地进行测试.

C#是否提供类似于SQL的语法糖,我不必重复被比较的变量或相等运算符?

c# syntactic-sugar

3
推荐指数
1
解决办法
158
查看次数

Tridion CoreService身份验证/模拟

我开发了一个使用核心服务的.Net库.此库是从工作流自动化决策中的VBScript调用的,并使用核心服务执行与该工作流程过程相关的一些活动.

我能够使用我们为Tridion提供的服务帐户成功连接到该服务:

CoreServiceClient client = new CoreServiceReference.CoreServiceClient(
                                                       binding, endpoint);
client.ChannelFactory.Credentials.Windows.ClientCredential = 
        new NetworkCredential(serviceAccountUsername, serviceAccountPassword);
client.ChannelFactory.Credentials.Windows.AllowedImpersonationLevel = 
        System.Security.Principal.TokenImpersonationLevel.Delegation;
Run Code Online (Sandbox Code Playgroud)

将相关绑定属性设置为以下内容:

binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
binding.Security.Transport.ClientCredentialType = 
        HttpClientCredentialType.Windows;
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是,当我调用核心服务时,我在CMS框上收到以下Tridion Content Manager错误:

用户NT AUTHORITY\NETWORK SERVICE拒绝访问.

如何配置我的客户端以便使用Tridion服务帐户而不是NT AUTHORITY\NETWORK SERVICE执行操作?

wcf tridion tridion-2011

3
推荐指数
1
解决办法
1952
查看次数

在Linq-to-Entities查询中转换为List <T>子类型

考虑三个类:

public class MyChildDto
{
    public int Id {get; set;}
    public string Name {get; set;}
}

public class MyChildDtos : List<MyChildDto>
{
    public MyChildDtos(List<MyChildDto> myChildDtos)
        : base(myChildDtos)
    {
    }
}

public class MyParentDto
{
    public int Id {get; set;}
    public MyChildDtos Children {get; set;}
}
Run Code Online (Sandbox Code Playgroud)

我还有一个我想查询并映射到父对象的实体集合,如:

public MyParentDto GetParentDto(int id)
{
    return DbContext.MyParentEntities.Select(p => new MyParentDto
        {
            Id = p.Id,
            Children = p.MyChildEntities.Select(c => new MyChildDto
            {
                Id = c.Id,
                Name = c.Name
            }).ToList()
        });
}
Run Code Online (Sandbox Code Playgroud)

(假设为了论证需要MyChildDtos类;我只是提出一个简化的例子.)

我遇到的问题是将MyChildEntitiesSelect …

c# linq-to-entities

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