小编Saa*_*adi的帖子

如何从Entity Framework 6中的Auditlog实体获取id

我知道那里有几个类似的帖子,但我找不到任何解决这个问题的方法.

我想在Entity Framework 6中添加,更改或删除实体(软删除)时添加(某种)AudioLog.我已经重写了SaveChanges,因为我只想为EntityStates添加,修改或删除的日志条目,我在第一次调用SaveChanges之前获取列表.问题是,因为我需要记录已执行的操作,我需要检查实体的EntityState.但是在调用SaveChanges之后,所有条目的EntityState都是Unchanged.

public override int SaveChanges()
{
    using (var scope = new TransactionScope())
    {
        var modifiedEntries = ChangeTracker.Entries()
            .Where(e => e.State == EntityState.Added || e.State == EntityState.Deleted || e.State == EntityState.Modified)
            .ToList();

        int changes = base.SaveChanges();
        foreach (var entry in modifiedEntries)
        {
            ApplyAuditLog(entry);
        }

        base.SaveChanges();
        scope.Complete();
        return changes;
    }
}

private void ApplyAuditLog(DbEntityEntry entry)
{
    ILog entity = entry.Entity as ILog;

    if (entity != null)
    {
        LogOperation operation;
        switch (entry.State)
        {
            case EntityState.Added:
                operation = LogOperation.CreateEntity;
                break;
            case …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework savechanges audit-logging entity-framework-6

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

C#wpf caliburn.Micro MahApps HamburgerMenu.ContentTemplate数据绑定不起作用

我正在使用Caliburn.Micro(用于简单的数据绑定和填充)和MahApps.Metro(用于设计)制作应用程序.

我创建了一个View名称'MainView',它有HamburgerMenuMahApps.我的问题是数据绑定在HamburgerMenu.ContentTemplate标签下工作正常

这是我的HamburgerMenu.ContentTemplatexaml.

<Page x:Class="Sample.Views.MainView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:cal="http://www.caliburnproject.org"
      xmlns:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
      xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
      xmlns:utils="clr-namespace:Omni.WindowsClient.Utils"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:local="clr-namespace:Omni.WindowsClient.Views"
      mc:Ignorable="d"
      d:DesignHeight="300"
      d:DesignWidth="600">

    <Page.Resources>
        <DataTemplate x:Key="HamburgerMenuItem"
                      DataType="{x:Type mah:HamburgerMenuItem}">
            <Grid Height="48">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="48" />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <Image Margin="12"
                       HorizontalAlignment="Center"
                       VerticalAlignment="Center"
                       Source="{Binding Glyph}"
                       Stretch="UniformToFill" />
                <TextBlock Grid.Column="1"
                           VerticalAlignment="Center"
                           FontSize="16"
                           Foreground="White"
                           Text="{Binding Label}" />
            </Grid>
        </DataTemplate>
    </Page.Resources>

    <Grid>

        <mah:HamburgerMenu x:Name="HamburgerMenuControl"
                           SelectedIndex="0"
                           ItemTemplate="{StaticResource HamburgerMenuItem}"
                           OptionsItemTemplate="{StaticResource HamburgerMenuItem}"
                           IsPaneOpen="True"
                           DisplayMode="CompactInline"
                           cal:Message.Attach="[Event ItemClick] = [Action ShowDetails(HamburgerMenuControl.SelectedItem)]"
                           DataContext="{Binding RelativeSource={RelativeSource self}}">
            <mah:HamburgerMenu.ItemsSource>
                <mah:HamburgerMenuItemCollection>
                    <mah:HamburgerMenuItem Label="System Status">
                        <mah:HamburgerMenuItem.Tag> …
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml caliburn.micro mahapps.metro

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

C#并行循环局部变量线程安全信息

我的问题很少理论我想知道List<object>如果我Parallel.For以这种方式使用是否是线程安全的.请看下面:

public static List<uint> AllPrimesParallelAggregated(uint from, uint to)
        {
            List<uint> result = new List<uint>();
            Parallel.For((int)from, (int)to,
                () => new List<uint>(), // Local state initializer
                (i, pls, local) =>      // Loop body
                {
                    if (IsPrime((uint)i))
                    {
                        local.Add((uint)i);
                    }
                    return local;
                },
                local =>                // Local to global state combiner
                {
                    lock (result)
                    {
                        result.AddRange(local);
                    }
                });
            return result;
        }
Run Code Online (Sandbox Code Playgroud)

local列表是线程安全的?我是否在result没有数据的情况下列表中的正确数据是由于多个线程正在改变,因为使用我正常循环?

注意:我不担心列表顺序.我想知道列表和数据的长度.

.net c# asp.net multithreading task-parallel-library

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

Dapper 映射多个连接 Sql 查询

我想将复杂对象映射到具有两个内部连接的查询的简洁结果。我知道我们有映射一个内部连接的解决方案,但我想映射两个内部连接结果。

这是场景:

我的课程是:

public class Order 
{
    public int id { get; set; }
    public string order_reference { get; set; }
    public string order_status { get; set; }
    public List<OrderLine> OrderLines { get; set; }
}

public class OrderLine
{
    public int id { get; set; }
    public int order_id { get; set; }
    public string product_number { get; set; }
    public List<OrderLineSize> OrderLineSizes { get; set; }
}

public class OrderLineSize
{
    public int id { get; set; } …
Run Code Online (Sandbox Code Playgroud)

c# sql mapping orm dapper

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

Power BI / 在报表视图上添加刷新按钮

我在 Power BI 方面没有太多专业知识。我开始研究它,发现它很容易,我已经创建了一个报告。该报告工作正常。

现在,我需要在报表上添加刷新按钮,该按钮将刷新数据源并从中获取最新数据。我试图对其进行大量搜索,但仍然找不到任何合理的解决方案。

我们在 Power BI 上有这种功能吗?

感谢您的时间!

.net business-intelligence powerbi

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

反序列化json时出现异常

我有2种方法

public static string SerializeObject<T>(T value)
{
    if (value == null)
    {
        return null;
    }

    var dictionaryObject = new Dictionary<string, object> { { typeof(T).Name, value } };

    var jsonString = JsonConvert.SerializeObject(dictionaryObject);

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

public static T DeserializeObject<T>(string jsonString)
{
    var objectValue = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonString);
    return JsonConvert.DeserializeObject<T>(objectValue.Values.First().ToString());
}
Run Code Online (Sandbox Code Playgroud)

当我用类型反序列化json字符串时

ConcurrentDictionary<KeyValuePair<long, long>, IList<string>>
Run Code Online (Sandbox Code Playgroud)

我有一个例外:

无法将字符串'[1,1]'转换为字典键类型'System.Collections.Generic.KeyValuePair`2 [System.Int64,System.Int64]'.创建TypeConverter以将字符串转换为键类型对象.路径'[1,1]',第2行,第12位.

那么有人能告诉我正确的代码吗?

这是我的代码:

var test = new ConcurrentDictionary<KeyValuePair<long, long>, IList<string>>();
test.TryAdd(new KeyValuePair<long, long>(1, 1), new List<string> { "Test" });

var se = SerializeObject(test);

var de …
Run Code Online (Sandbox Code Playgroud)

c# asp.net json deserialization json-deserialization

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

将类List列表项匹配的Dictionary键加入新列表 - C#

所以我有一个Dictionary字符串值作为键.我有一个班级列表:

listCoord = new List<Coord>();
Run Code Online (Sandbox Code Playgroud)

其班级Coord如下:

class Coord
{
    public string Segment { get; set; }
    public double startX { get; set; }
    public double startY { get; set; }
    public double endX { get; set; }
    public double endY { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

它有大约12000 string Segment的.我的词典Keys是其中一些细分市场.现在我需要将我的字典中的段与那些中的坐标值连接起来List.

起初我使用了使用方法并通过foreach每个字典键将其与列表段进行比较以找到匹配项.然后我了解到LINQ可以使用SQL的内部关节来做同样的事情.

问题:

  1. 查找词典键和某些列表项之间匹配的最佳方法是什么?
  2. 一旦我这样做,我怎么把它全部放到另一个包含匹配segment及其相应startX, startY, endX, endY值作为列表项的列表中.

如果已经提出并回答了这样的问题,我会提前道歉; 个人找不到它.

c# linq dictionary list

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

C#Azure Blob存储/获取blob大小

我已经开始研究Azure存储了.我已经实现了创建/删除容器,创建/删除/ isExist blob功能.但我很惊讶从Azure存储中找到当前的Blob大小.

我做了很多研究,但我找不到任何合理的解决方案.

这是获取Blob大小的函数:

public long GetBlockBlobSize(string containerName, string blobName)
        {
            try
            {
                CloudBlobContainer container = _blobClient.GetContainerReference(containerName);
                var blockBlob = container.GetBlockBlobReference(blobName);
                return blockBlob.Properties.Length;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Run Code Online (Sandbox Code Playgroud)

blockBlob.Properties.Length 值为-1.

  • 这种方法有什么问题吗?
  • 有更好的方法吗?

谢谢!

c# azure azure-storage

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

如何将子对象从 JArray 获取到 ObservableCollection

我正在为 Windows Phone 开发一个应用程序,其中列表框显示 JSON 文件中的数据。我正在使用 aJArray并且可以根据数组位置显示数据。但是,如果我想显示 JSON 文件中的所有数据(该文件没有静态数据,并且数据稍后可能会被修改)怎么办?

我的 JSON:

[
    {
        "xId": "52",
        "result": {
            "type": "Basico.Bean.MunicipioClass.TMunicipio",
            "id": 1,
            "fields": {
                "FRefCount": 0,
                "FId": 52,
                "FNome": "Sumare",
                "FEstado": "SP",
                "FPais": "Brasil"
            }
        }
    },
    {
        "xId": "52",
        "result": {
            "type": "Basico.Bean.MunicipioClass.TMunicipio",
            "id": 1,
            "fields": {
                "FRefCount": 0,
                "FId": 52,
                "FNome": "Indaiatuba",
                "FEstado": "SP",
                "FPais": "Brasil"
            }
        }
    }
]
Run Code Online (Sandbox Code Playgroud)

我的代码:

InitializeComponent();

String text;


using (var store = IsolatedStorageFile.GetUserStoreForApplication())
using (var readStream = new IsolatedStorageFileStream("json.html", FileMode.Open, FileAccess.Read, …
Run Code Online (Sandbox Code Playgroud)

c# json json.net windows-phone-8

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

如何初始化Task <List <string >>参数?

我正在尝试为函数编写单元测试,但我需要在测试之前启动一些变量.

我需要测试的函数有一个参数 Task<List<string>> list

我想发起这个变量.

这是我到目前为止所尝试的:

Task<List<string>> list = new List<string>();
Run Code Online (Sandbox Code Playgroud)

c# task-parallel-library

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