Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在.Net Core中使用主键在两行中添加相同的值_C#_.net_Entity Framework_Entity - Fatal编程技术网

C# 如何在.Net Core中使用主键在两行中添加相同的值

C# 如何在.Net Core中使用主键在两行中添加相同的值,c#,.net,entity-framework,entity,C#,.net,Entity Framework,Entity,我有如下数据库表: [Id] INT IDENTITY (1, 1) NOT NULL, [SecondId] INT NOT NULL, 创建新行时,我想沿第一行使用[Id]在第二行的[SecondId]中添加相同的值,如 1 1 (first raw) 2 1 (second raw) [id is different, but secondId is same] 3 3 (first raw) 4 3 (second raw) [id is different, but se

我有如下数据库表:

[Id]       INT IDENTITY (1, 1) NOT NULL,
[SecondId] INT NOT NULL,
创建新行时,我想沿第一行使用
[Id]
在第二行的
[SecondId]
中添加相同的值,如

1 1 (first raw)
2 1 (second raw) [id is different, but secondId is same]
3 3 (first raw)
4 3 (second raw) [id is different, but secondId is same]
我希望在使用HttpPost添加新行时会导致此操作

你能帮我吗

总之:

  • 我想在使用HttpPost时创建两行

  • 第二行的
    [secondId]
    与第一行的
    [id]
    具有相同的值


  • 这里有一个方法可以帮助你解决问题的逻辑部分。它获取一个包含
    PrimaryId
    SecondaryId
    的项目列表,并将下一个应添加到该列表中的项目返回给您

    首先,这里是我们正在使用的
    类:

    public class Item
    {
        public int PrimaryId { get; set; }
        public int SecondaryId { get; set; }
    }
    
    该方法通过查看列表中的最后一项(
    lastItem
    ),计算出要分配给下一项的
    PrimaryId
    SecondaryId
    )的内容。然后,它使用
    PrimaryId
    创建一个新项目,即
    lastItem.PrimaryId+1
    ,并根据
    lastItem.PrimaryId
    是否为奇数或偶数分配
    SecondaryId

    请注意,如果上一项的
    PrimaryId
    为偶数,则下一项的
    SecondaryId
    等于上一项的
    PrimaryId+1
    。否则它等于上一项的
    PrimaryId

    方法如下:

    public static Item GetNextItem(List<Item> items)
    {
        // If there are no existing items, return the default first item
        if (items == null || items.Count == 0) 
        {
            return new Item {PrimaryId = 1, SecondaryId = 1};
        }
    
        // Otherwise, grab the last item in the list
        var lastItem = items[items.Count - 1];
    
        // And use it's PrimaryId to determine the values for the next item's properties
        return new Item
        {
            PrimaryId = lastItem.PrimaryId + 1,
            SecondaryId = lastItem.PrimaryId % 2 == 0
                ? lastItem.PrimaryId + 1
                : lastItem.PrimaryId
        };
    }
    
    公共静态项GetNextItem(列表项)
    {
    //如果没有现有项,则返回默认的第一项
    if(items==null | | items.Count==0)
    {
    返回新项{PrimaryId=1,SecondaryId=1};
    }
    //否则,抓取列表中的最后一项
    var lastItem=items[items.Count-1];
    //并使用它的PrimaryId来确定下一项属性的值
    返回新项目
    {
    PrimaryId=lastItem.PrimaryId+1,
    SecondaryId=lastItem.PrimaryId%2==0
    ?lastItem.PrimaryId+1
    :lastItem.PrimaryId
    };
    }
    
    现在正在使用:

    private static void Main()
    {
        var items = new List<Item>();
    
        // Populate the list of items using our method to continually get the next item
        for (int i = 0; i < 4; i++)
        {
            items.Add(GetNextItem(items));
        }
    
        // Now output the items to the Console window
        foreach (var item in items)
        {
            Console.WriteLine($"{item.PrimaryId} {item.SecondaryId}");
        }
    
        GetKeyFromUser("\nDone! Press any key to exit...");
    }
    
    private static void Main()
    {
    var items=新列表();
    //使用我们的方法填充项目列表,以持续获取下一个项目
    对于(int i=0;i<4;i++)
    {
    items.Add(GetNextItem(items));
    }
    //现在将项目输出到控制台窗口
    foreach(项目中的var项目)
    {
    WriteLine($“{item.PrimaryId}{item.SecondaryId}”);
    }
    GetKeyFromUser(“\n完成!按任意键退出…”);
    }
    
    输出


    这与C有什么关系?你试过什么了吗?你的努力是什么?这个问题太广泛了,有各种各样的问题。感觉上你希望有人给你写一个完整的应用程序。表现出努力,带着具体问题回来。@DavidG我还没有尝试过,因为我们不知道。。。我想创建1-1聊天室数据库,每行有2个用户,secondid是roomIdEither,或者更精确地重新表述它。所涉及的逻辑可能不依赖于数据库或httpPost。你有一个包含两个int属性的对象列表,你想…。@P.Brian.Mackey是的,我知道我很粗鲁。。。但真的不知道;;很抱歉