ASP.net建立多对多实体在C# [英] ASP.net create many to many entity in C#

查看:90
本文介绍了ASP.net建立多对多实体在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创造一个许多人在asp.net C#中的许多实体。我看过前学员/课程的例子,这是合乎逻辑但事情是错的字面当我尝试在我自己的code使用它。

I'm trying to create an many to many entity in asp.net C#. I have looked at the student/course example before and it is logical but something is literally wrong when I try use it in my own code.

下面是code,其中我卡住了。

Here is the code where I'm stuck.

    [HttpPost]
    public ActionResult BuyWeapon(int weaponId)
    {
        string currentUserId = User.Identity.GetUserId();

        var ctx = new DAL.ProjectStrawberryEntities();

        Character character = ctx.Characters.FirstOrDefault(c => c.AccountId == currentUserId);
        Weapon weapon = ctx.Weapons.FirstOrDefault(w => w.Id == weaponId);

        if (character.Gold - weapon.Price >= 0)
        {
            // Add weapon to character

            character.Gold -= (int)weapon.Price;
        }

        return View();
    }

的属性只有字符武器已经为 WeaponCharacter ,这是我为武器字符

The only properties Character and Weapon has is WeaponCharacter, which is my bridge table for Weapon and Character.

下面是我的字符武器模型从我的数据库。

Here are my Character and Weapon models from my database.

public partial class Character
{
    public Character()
    {
        this.WeaponCharacters = new HashSet<WeaponCharacter>();
    }

    *Removed wall of propeties*

    public virtual AspNetUser AspNetUser { get; set; }
    public virtual Class Class { get; set; }
    public virtual Gender Gender { get; set; }
    public virtual ICollection<WeaponCharacter> WeaponCharacters { get; set; }
}

public partial class Weapon
{
    public Weapon()
    {
        this.WeaponCharacters = new HashSet<WeaponCharacter>();
    }

    *Removed wall of propeties*

    public virtual WeaponType WeaponType { get; set; }
    public virtual ICollection<WeaponCharacter> WeaponCharacters { get; set; }
}

public partial class WeaponCharacter
{
    public int CharacterId { get; set; }
    public int WeaponId { get; set; }
    public int Quantity { get; set; }

    public virtual Character Character { get; set; }
    public virtual Weapon Weapon { get; set; }
}

我冒出了一些特性,因为它们是不必要的。结果

不应该字符有一个的ICollection&LT;&支招GT; ?我错了用一个多对多的关系这个?

Shouldn't Character have an ICollection<Weapon>? Am I wrong to use a many to many relation for this?

我想达到的目标是:我想要一个字符购买的武器,就可以买好几相同的武器,但每点击一次

The goal I'm trying to reach is: I want a character to buy a weapon, it is possible to buy several of the same weapon but only once per click.

推荐答案

因为我已经习惯了的EntityFramework为code-首先的形式给出我可能是错的,但应该不是你的字符类有一个的ICollection&LT;&支招GT; 属性,而不是WeaponCharacter?我的经验(有EF,最新版本)的ICollection的将被自动映射到你项目添加到该集合的WeaponCharacter表。

I might be wrong as I'm used to EntityFramework for Code-First aproach, but shouldn't your Character class have an ICollection<Weapon> property instead of 'WeaponCharacter'? On my experience (With EF, latest version) the ICollection would be automatically mapped to the WeaponCharacter table as you add items to that collection.

示例工作多到多的关系,采用EF-codeFirst,其中<​​code> prescricao 与<一个多一对多的关系code> Remedio :

Example of a working many-to-many relationship, using EF-CodeFirst, where Prescricao has a many-to-many relationship with Remedio:

public class Prescricao
{
    // Hidden Properties

    /// <summary>
    /// Remédios prescritos para o paciente.
    /// </summary>
    public virtual ICollection<Remedio> Remedios { get; set; }

    public Prescricao()
    {
        Remedios = new List<Remedio>();
    }

}

public class Remedio
{
    /// <summary>
    /// Prescrições que incluem o remédio.
    /// </summary>
    public virtual ICollection<Prescricao> Prescricoes { get; set; }

    public Remedio()
    {
        Prescricoes = new List<Prescricao>();
    }

添加东西的关系:

var pres1 = new Classes.Prescricao()
        {
            DataEmissao = DateTime.Now,
            DataLimite = DateTime.Now.AddMonths(1),
            Descricao = "Prescrição teste.",
            Medico = m1,
            Paciente = p1,
            Status = Classes.PrescricaoStatus.Pendente
        };
var r1 = new Classes.Remedio()
        {
            NomeComercial = "Aspirina",
            PrecoMedio = 12.49m,
            PrincipioAtivo = "Whatever",
            Status = Classes.RemedioStatus.Aprovado
        };

        context.Remedios.Add(r1);
        pres1.Remedios.Add(r1);
        context.Prescricoes.Add(pres1);

(本来要添加为评论,因为我不认为这解决了问题,在手,但我不能发表评论,只是还没有)

(Was going to add this as a comment as I don't think this solves the issue at hand but I can't comment just yet)

这篇关于ASP.net建立多对多实体在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆