L
L
lena_shevtsova2021-11-23 22:31:43
ASP.NET
lena_shevtsova, 2021-11-23 22:31:43

Why do database errors occur when developing a web on mvc5?

RealtyContext.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace realtyStore.Models
{
    public class RealtyContext : DbContext
    {
        public DbSet<Realty> Realties { get; set; }
        public DbSet<Buyer> Buyers { get; set; }
        public DbSet<Realtor> Realtors { get; set; }
        public DbSet<Owner> Owners { get; set; }
        public DbSet<Sold> Sold { get; set; }
        public DbSet<Leased> Leased { get; set; }
        public DbSet<City> Cities { get; set; }

    }
}

HomeController.cs
namespace realtyStore.Controllers
{
    public class HomeController : Controller
    {
        RealtyContext db = new RealtyContext();
        public ActionResult Index()
        {
            IEnumerable<City> cities = db.Cities;
            IEnumerable<Realty> realties = db.Realties;
            ViewBag.Realties = realties;
            ViewBag.Cities = cities;
            return View();

        }
        [HttpGet] 
        public ActionResult Flat(int? id)
        {
            Realty realty = null;
            foreach(var r in db.Realties)
            {
                if (id != null && id == r.Id)
                {
                    realty = r;
                }
            }
            ViewBag.Realty = realty;
            return View();
        }

        [HttpPost]
        protected string Search(string city, string[] NumberRoom, string RealtyType)
        {
            return city + ' ' + RealtyType + ' ' + NumberRoom[0];
        }

    }
}

Home/Index.cshtml
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
    ViewBag.Title = "Home Page";
    var RealtyType = "";
    string[] NumberRoom;

    if (IsPost)
    {
        RealtyType = Request.Form["RealtyType"];
        NumberRoom = Request.Form.GetValues("NumberRoom[]");
    }
}

 @*@Html.partial("_realtyitemslist");*@ пишет, что html не существует в данном контексте. ?

<div>
    <h3>Поиск недвижимости</h3>

    <div class="search-form">
        <form id="for-sale" method="post">

            <select class="form-select" aria-label="Default select example" name="RealtyType" style="width:100px">
                <option value[email protected]>@realtyStore.Models.Types.APARTMENT</option>
                <option value[email protected]>@realtyStore.Models.Types.ROOM</option>
                <option value[email protected]>@realtyStore.Models.Types.HOUSE</option>
                <option value[email protected]>@realtyStore.Models.Types.BED</option>
            </select>
            <div class="checkBox-group">
                Количество комнат
                <div class="form-check form-check-inline">
                    <input class="form-check-input" type="checkbox" value="1" id="flexCheckDefault" name="NumberRoom[]">
                    <label class="form-check-label" for="flexCheckDefault">
                        1
                    </label>

                    <input class="form-check-input" type="checkbox" value="2" id="flexCheckChecked" name="NumberRoom[]" checked>
                    <label class="form-check-label" for="flexCheckChecked">
                        2
                    </label>

                </div>
            </div>
            <select class="form-select" aria-label="Default select example" name="City" style="width:100px">
                @foreach (var c in ViewBag.Cities)
                {
                    <option value[email protected]>@c.Name</option>
                }
            </select>
            <input id="Button1" type="submit" value="Поиск" />
        </form>

    </div>

    @foreach (var r in ViewBag.Realties)
    {
        <div class="realtyItem">
            <div class="realtyItem__img">
                <a href="Home/Flat/@r.Id"><img [email protected] alt="photo" /></a>
            </div>

            <div>
                <a href="Home/Flat/@r.Id"><p class="realtyItem__title">@r.Status @r.Type</p></a>
                <p class="realtyItem__desc_mini">
                    @if (r.NumberRoom != null)
                    {<span>@r.NumberRoom -комн. </span>}
                    @r.Type, r.Squre кв.м.,
                    @if (@r.Floor != null)
                    {<span> @r.Floor /</span>} @r.Floors эт.
                </p>

                <p class="realtyItem__price"> @r.Price руб.</p>
            </div>
        </div>
    }
</div>

Exception thrown: 'System.Data.SqlClient.SqlException' in EntityFramework.dll
An exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll but was not handled in user code
Cannot drop database 'realtyStore.Models.RealtyContext' because it is currently in use.

Home/Flat.cshtml
Realty not coming to view
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="realtyItem">
    
    <div class="realtyItem_left">
        <img [email protected] alt="photo" />
        <p class="realtyItem__title">
            @ViewBag.Realty.Status

            @if (ViewBag.Realty.NumberRoom != null)
            {<span>@ViewBag.Realty.NumberRoom -комн. </span>}
            @ViewBag.Realty.Type, r.Squre кв.м.,@if (@ViewBag.Realty.Floor != null)
            {<span> @ViewBag.Realty.Floor /</span>} @ViewBag.Realty.Floors эт.

        </p>

        <p class="realtyItem__desc">@ViewBag.Realty.Description</p>

    </div>
    <div class="realtyItem_right">
        <p class="realtyItem__price">@ViewBag.Realty.Price </p>
        <asp:Button [email protected] runat="server" Text="Показать номер телефона" OnClick="ShowPhone_Click" />
    </div>
</div>

not yet picked up by css that is not bootstrap. Why?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-11-24
@vabka

It's written in the error:


Cannot drop database "realtyStore.Models.RealtyContext" because it is currently in use.

So you're trying to DROP DATABASE somewhere when it's in use.
By the way, the context should be disposed after use. Otherwise, you will have a leak.
Usually the DI container
does this, but this shnyaga will cause you to enumerate all the data in the database
foreach(var r in db.Realties)
            {
                if (id != null && id == r.Id)
                {
                    realty = r;
                }
            }

Better to replace with
ViewBag.Realty = db.Rwalties.FirstOrDefault(x=>x.Id ==id);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question