首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建新的数据库条目并使用视图显示

问创建新的数据库条目并使用视图显示
EN

Stack Overflow用户
提问于 2021-06-03 03:27:41
回答 1查看 62关注 0票数 0

我不知道我的密码是在哪里破的。cshtml页面没有显示ViewBag列表。我不知道当我在列表中添加新的参与者时,还是当我调用要显示的列表时,它是否会被打破

基本上,我想做的是列一个篮球参与者的名单。如果某个年龄组和性别的列表不存在,我的代码应该创建列表,然后将参与者添加到其中。如果年龄组和性别的名单已经存在,则只需将参与者添加到该名单中即可。

该守则所做的是:

  • 得到一个id

。

  • 如果id与参与者的id匹配,则检查参与者

的性别。

  • 检查参与者

的年龄

  • 检查这些标准是否存在联盟模型。如果没有,将为性别和年龄组

创建一个新的联盟模式。

  • 创建了一个新的多对多模型,其中participantId = id传递到模型中,leagueId =联盟模型的id,我刚刚创建了

。

然而,

  • ,如果联盟模式已经存在于年龄组/性别,只需添加多到多模式

即可。

主计长:

代码语言:javascript
复制
[HttpPost]
[Route("postNewBasketballPlayer")]
public IActionResult PostNewBasketballPlayer(int newParticipantId)
{
    ViewModel.Participant newP = db.Participants.FirstOrDefault(i=>i.ParticipantId == newParticipantId);

    if (newP != null)
    {    
        var timespan = DateTime.Now - newP.ParticipantDOB;

        // this block of code is also repeated for newP.ParticipantGender == "Female"
        if (newP.ParticipantGender == "Male")
        {
             Console.WriteLine("Male basketball player");
             // this block of code repeats for different age groups
             if (timespan.TotalDays >= (7 * 365) && timespan.TotalDays < (9 * 365))
             {
                 League LeaguebbM7and8 = db.Leagues.FirstOrDefault(i=>i.gender == "Male" && i.ageRange == "7and8" && i.sport == "Basketball");

                 // if the League doesn't exist it creates it
                 if (LeaguebbM7and8 == null)
                 {
                     LeaguebbM7and8 = new League()
                     {
                         sport = "Basketball",
                         gender = "Male",
                         ageRange = "7and8"
                     };

                     db.Add(LeaguebbM7and8);
                     db.SaveChanges();

                     Console.WriteLine("made new league LeaguebbM7and8");

                     MMLeagueParticipant MMbbM7and8 = new MMLeagueParticipant()
                     {
                         ParticipantId = newParticipantId,
                         LeagueId = LeaguebbM7and8.LeagueId
                     };

                     // adds a row to table with the id's
                     db.Add(MMbbM7and8);
                     db.SaveChanges();

                     Console.WriteLine("added to LeaguebbM7and8");
                 }
                 // if the league exists; it simply adds it. 
                 else
                 {
                     // each time I add a child, it needs its own middle table (reason for new)
                     MMLeagueParticipant MMbbM7and8 = new MMLeagueParticipant()
                     {
                         ParticipantId = newParticipantId,
                         LeagueId = LeaguebbM7and8.LeagueId
                     };

                     db.Add(MMbbM7and8);
                     db.SaveChanges();

                     Console.WriteLine("added to LeaguebbM7and8");
                 }

                 Console.WriteLine("returning to dashboard (LeaguebbM7and8)");
                 return RedirectToAction("Dashboard", "Dashboard");
             }

             // this code is repeated for multiple age checks as above.
             // else if (timespan.TotalDays >= (9 * 365) && timespan.TotalDays < (11 * 365)){...}
             // else if (timespan.TotalDays >= (11 * 365) && timespan.TotalDays < (13 * 365)){...}
             // else if (timespan.TotalDays >= (13 * 365) && timespan.TotalDays < (15 * 365)){...}
             // else if (timespan.TotalDays >= (15 * 365) && timespan.TotalDays < (17 * 365)){...}
             // else if (timespan.TotalDays >= (17 * 365) && timespan.TotalDays < (19 * 365)){...}
         }

         // repeats the block of code above for all the age check if female.
         // if (newP.ParticipantGender == "Female)...
     }

     if (newP == null)
     {
         Console.WriteLine("id passed in was null");
         return RedirectToAction("Dashboard", "Dashboard");
     }

     Console.WriteLine("skips all if checks because newP was   null");
     return RedirectToAction("Dashboard", "Dashboard");
}

当LeaguebbM7and8为null时,我的终端读取的内容:

代码语言:javascript
复制
    newParticipantId
    15
    newP
    LeagueProject.Models.ViewModel+Participant
    Male basketball player
    made new league LeaguebbM7and8
    added to LeaguebbM7and8
    returning to dashboard (LeaguebbM7and8)

当该性别和年龄组的列表已经存在时,我的终端读取的内容(LeageuebbM7and8不为空):

代码语言:javascript
复制
newParticipantId
15
newP
LeagueProject.Models.ViewModel+Participant
Male basketball player
added to LeaguebbM7and8
returning to dashboard (LeaguebbM7and8)

我的联盟模型:

代码语言:javascript
复制
int LeagueId { get; set; }
string sport { get; set; }
string gender { get; set; }
string ageRange { get; set; }
​List<MMLeagueParticipant> allParticipants  { get; set; }        //(a many to many relationship for league and participant)

我的ViewModel模型:

代码语言:javascript
复制
public List<Participant> allParticipants { get; set; }
public ViewModel.Participant participant { get; set; }

public class Participant
{
    int ParticipantId { get; set; }
    string ParticipantFirstName { get; set; }
    string ParticipantLastName { get; set; }
    string ParticipantGender { get; set; }
    SystemDateTime ParticipantDOB { get; set; }
    List<MMLeagueParticipant> all Leagues { get; set; }
}

我的MMLeagueParticipant中位联赛模型和ViewModel.Participant:

代码语言:javascript
复制
int MMLeaugeParticipantId { get; set; }
int ParticipantId { get; set; }
int LeaugeId { get; set; }
leauge sport { get; set; }
ViewModel.Participant child { get; set; }

使用上面列出的模型;在调用要显示的页面时,我试图通过以下操作查询和获取参与者列表:

代码语言:javascript
复制
[HttpGet]
[Route("roster/basketball")]
public IActionResult BasketballRoster()
{
    //the reason im using viewbag is because its easier to work with since I can't pass multiple models in. and everything is in tabs. just makes life easier

    ViewBag.bbM7and8 = db.Leagues.Where(i=>i.sport == "Basketball" && i.ageRange == "7and8").ToList();
    Console.WriteLine(ViewBag.bbM7and8);

    ViewBag.bbM9and10 = db.Leagues.Where(i=>i.sport == "Basketball" && i.ageRange == "9and10").ToList();
    Console.WriteLine(ViewBag.bbM9and10);

    ViewBag.bbM11and12 = db.Leagues.Where(i=>i.sport == "Basketball" && i.ageRange == "11and12").ToList();
    Console.WriteLine(ViewBag.bbM11and12);

    ViewBag.bbM13and14 = db.Leagues.Where(i=>i.sport == "Basketball" && i.ageRange == "13and14").ToList();
    Console.WriteLine(ViewBag.bbM13and14);

    ViewBag.bbM15and16 = db.Leagues.Where(i=>i.sport == "Basketball" && i.ageRange == "15and16").ToList();
    Console.WriteLine(ViewBag.bbM15and16);

    ViewBag.bbM17and18 = db.Leagues.Where(i=>i.sport == "Basketball" && i.ageRange == "17and18").ToList();
    Console.WriteLine(ViewBag.bbM17and18);

    return View("RosterPageBasketball");
}

console.WriteLine(ViewBag.bbM7and8)在终端中显示的内容:

代码语言:javascript
复制
System.Collections.Generic.List`1[LeagueProject.Models.League]

我的名册页包含不同的选项卡。每个选项卡都显示“ViewBag.bbM#and#”(及其受尊敬的年龄组)和“测试1”。

代码语言:javascript
复制
            //cshtml

            <div class="tab-content" id="nav-tabContent">
                <div class="tab-pane fade" id="bbM7and8" role="tabpanel" aria-labelledby="bbM7and8-tab">
                    7-8
                    @{
                        var hasData=false;
                        //checks if list is null
                        if(ViewBag.bbM7and8 != null)
                        {
                            <p>test: ViewBag.bbM7and8 is not null</p>
                            @foreach(League j in ViewBag.bbM7and8)
                            {
                                <p>test 1: foreach</p>
                                @if( j.allParticipants != null) 
                                {
                                    <p>test 2: null</p>
                                    @foreach(MMLeagueParticipant mmLp in j.allParticipants)
                                    {
                                        <p>test 3: foreach</p>
                                        @if( mmLp.child != null) 
                                        {
                                            p>test 4: null</p>
                                            hasData=true;
                                            <p>@mmLp.child.ParticipantFirstName @mmLp.child.ParticipantLastName</p>
                                        }
                                    }
                                }
                                @if( j.allParticipants == null)
                                {
                                    <p>league model is null</p>
                                }
                            }
                        }
                        else
                        {
                            @if(!hasData)
                            {
                                <p>the list is empty</p>
                            }
                        }
                    }
                </div>
                //the above code is repeated for each tab
                <div class="tab-pane fade" id="bbM9and10" role="tabpanel" aria-labelledby="bbM9and10-tab"></div>
                <div class="tab-pane fade" id="bbM11and12" role="tabpanel" aria-labelledby="bbM11and12-tab"></div>
                <div class="tab-pane fade" id="bbM13and14" role="tabpanel" aria-labelledby="bbM13and14-tab"></div>
                <div class="tab-pane fade" id="bbM15and16" role="tabpanel" aria-labelledby="bbM15and16-tab"></div>
                <div class="tab-pane fade" id="bbM17and18" role="tabpanel" aria-labelledby="bbM17and18-tab"></div>

页面显示的内容:

代码语言:javascript
复制
test b
7-8
test: ViewBag.bbM7and8 is not null

test 1: foreach
league model is null

没有测试3,这意味着ViewBag中的每个联盟都是空的。这让我相信问题可能在于我是如何创造和加入联盟模式的。在我看来一切都很好。只需要第二双眼睛。

注:我应该有两个参与者在这个视袋。

我试过的是:

我试着将这两个前端放在under下面,以查看是否可以对空引用执行运行时绑定,inversely.

  • error: RuntimeBinderException: RuntimeBinderException:无法对空引用执行运行时绑定。
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-03 06:26:48

问题是您的代码没有显式加载allParticipants。

实体框架和Linq到-实体不会神奇地从数据库加载整个对象图,您需要告诉它何时加载和加载什么。

此外,您应该始终倾向于显式加载和禁用EF的延迟加载功能--这是因为现代(并且正确)的方法意味着您不能在属性获取器中执行任何IO (因为副作用很糟糕,IO通常涉及async/await,并且不能在属性getter中使用await )。

您还可以极大地简化加载所有篮球League实体的逻辑,给出一个AgeRange值列表--因此,下面,除了显式加载所有相关MMLeagueParticipants和Participants实体的代码之外,还可以这样做:

将加载代码更改为:

代码语言:javascript
复制
using System.Data.Entity;

[HttpGet]
[Route("roster/basketball")]
public async Task<IActionResult> GetBasketballRoster()
{
    String[] leagueNames = new[]
    {
        "7and8",
        "9and10",
        "11and12",
        "13and14",
        "15and16",
        "17and18"
    };

#if NEVER_DO_THIS

    // The *bad* way is to load the Leagues then manually load each League's participants individually - this will result in `1 + M` queries, or even `1 + M + ( M * P )` queries.

    List<League> bbLeagues = await db.Leagues
        .Where( l => l.Sport == "Basketball" )
        .Where( l => leagueNames.Contains( l.AgeRange ) )
        .ToListAsync();    

    
    foreach( League league in bbLeagues )
    {
        /*
        await db.Entry( league )
            .Collection( l => l.AllParticipants )
            .LoadAsync(); // Note this will not load `Participant`.
        */

        // This code does load `Participant` but we need to use `Query()`, but when you use `Query` EF isn't smart enough to know how to track the entities in-memory, so you need to manually add everything:

        List<MMLeagueParticipants> leagueParticipants = await db.Entry( league )
            .Collection( l => l.AllParticipants )
            .Query() // <-- This is needed to allow for `Include()`
            .Include( mmp => mmp.Participant )
            .ToListAsync();

        // Need to do this because EF won't do it for us when using `Query()`:
        league.AllParticipants.AddRange( leagueParticipants );
    }

#else

    // A better approach is to batch-load each entity (Leagues, MMLeagueParticipants, and Participants), resulting in only 3 queries:
    
    List<League> bbLeagues = await db.Leagues
        .Where( l => l.Sport == "Basketball" )
        .Where( l => leagueNames.Contains( l.AgeRange ) )
        .ToListAsync();    

    List<MMLeagueParticipants> links;
    if( true /* Using a manual JOIN: */ )
    {
        
        List<Int32> leagueIds = bbLeagues.Select( l => l.LeagueId ).ToList();

        links = await db.MMLeagueParticipants
            .Where( mmp => leagueIds.Contains( mmp.LeagueId ) )
            .ToListAsync();
    }
    else // Using a DB JOIN, assuming your Navigation Properties are configured correctly:
    {
        links = await db.MMLeagueParticipants
            .Where( mmp => mmp.League.Sport == "Basketball" )
            .Where( mmp => leagueNames.Contains( mmp.League.AgeRange ) )
            .ToListAsync();
    }

    List<Int32> participantIds = participants
        .Select( p => p.ParticipantId )
        .Distinct() // <-- This step prevents duplicate results.
        .ToList();

    List<Participant> participants = await db.Participants
        .Where( p => participantIds.Contains( p => p.ParticipantId ) )
        .ToListAsync();

    //

#endif

    // NOTE: Entity Framework automagically "loads" entities it reads from the database directly into the DbContext *and other loaded entities*, so after the above manual loading then all of the `League` objects will have correctly-populated `Participants` collections - but here's an assertion anyway:
    if( participants.Count > 0 && bbLeagues.All( l => l.AllParticipants == null || l.AllParticipants.Count() == 0 )
    {
        throw new InvalidOperationException( "No participants collections were updated by Entity Framework after loading participants. Is your model configured correctly?" );
    }

    IReadOnlyDictionary< String, League > leaguesDict = bbLeagues.ToDictionary( l => l.AgeRange );

    RosterPageViewModel pageViewModel = new RosterPageViewModel( leaguesDict );

    return View( "RosterPageBasketball", pageViewModel );
}

public class RosterPageViewModel
{
    public RosterPageViewModel(
        IReadOnlyDictionary< String, League > leagues
    )
    {
        this.Leagues = leagues ?? throw new ArgumentNullException(nameof(leagues));
    }

    public IReadOnlyDictionary< String, League > Leagues { get; }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67814987

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档