Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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
Asp.net mvc AspNet标识中的配置文件图像_Asp.net Mvc_Entity Framework_Asp.net Mvc 5_Asp.net Identity_Edmx - Fatal编程技术网

Asp.net mvc AspNet标识中的配置文件图像

Asp.net mvc AspNet标识中的配置文件图像,asp.net-mvc,entity-framework,asp.net-mvc-5,asp.net-identity,edmx,Asp.net Mvc,Entity Framework,Asp.net Mvc 5,Asp.net Identity,Edmx,在我的应用程序中,我希望在布局页面(\u LoginPartial)中显示用户配置文件图像 在AspNet Identity中,他们的成员身份是AspNerUser表。我想自定义此AspNerUser表以维护图像字段 然后在布局页面(\u LoginPartial)视图中显示该图像 我该怎么做?真的很感激你能提出一个方法来做到这一点 编辑 我使用ADO.NET实体模型生成了我的DataBaseContext名称,数据库上下文名称是ProjectNameEntities 然后,我尝试在PMC上使用

在我的应用程序中,我希望在布局页面(\u LoginPartial)中显示用户配置文件图像

AspNet Identity
中,他们的成员身份是
AspNerUser
表。我想自定义此
AspNerUser
表以维护图像字段

然后在布局页面(
\u LoginPartial
)视图中显示该图像

我该怎么做?真的很感激你能提出一个方法来做到这一点

编辑

我使用ADO.NET实体模型生成了我的DataBaseContext名称,数据库上下文名称是
ProjectNameEntities

然后,我尝试在PMC上使用以下命令启用迁移

启用迁移-ContextTypeName myProject.Models.ProjectNameEntities

但我得到了以下错误

创建DbModelBuilder或从创建的DbContext写入EDMX 不支持先使用数据库或先使用模型。EDMX只能是 从在不使用现有数据库的情况下创建的代码优先DbContext中获取 DbCompiledModel

edmx模型可以这样做吗?

添加字段模型(先编码) 您需要做的第一件事是修改构建数据库表的ApplicationUser模型。此类通常位于
IdentityModels.cs
文件中。添加新字段以保存图像:

public class ApplicationUser : IdentityUser
{
   // maybe be other code in this model
    public byte[] ProfilePicture { get; set; }
}
接下来,您需要更新数据库以反映更改(假设您首先使用代码)。您可以在中找到有关该过程的详细信息

返回配置文件图片 现在向控制器添加一个类似于以下内容的操作:

public FileContentResult Photo(string userId)
{
    // get EF Database (maybe different way in your applicaiton)
    var db = HttpContext.GetOwinContext().Get<ApplicationDbContext>();

    // find the user. I am skipping validations and other checks.
    var user = db.Users.Where(x => x.Id == userId).FirstOrDefault();

    return new FileContentResult(user.ProfilePicture, "image/jpeg");
}
保存个人资料图片 首先,您需要创建一个页面来上传图像。创建一个操作以返回表单:

[HttpGet]
public ActionResult Profile()
{
    ViewBag.Message = "Update your profile";
    return View();
}
Razor视图将被称为Profile.cshtml,其上的表单如下:(注意,动作和控制器的位置可能因您构建项目的方式而异)

@使用(Html.BeginForm(“Profile”,“Manage”,FormMethod.Post,new{enctype=“multipart/formdata”}))
{
@Html.AntiForgeryToken()
照片
文件名:

}
表单将发回一个操作,因此您需要创建一个如下所示的操作:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Profile(HttpPostedFileBase Profile)
{
    // get EF Database (maybe different way in your applicaiton)
    var db = HttpContext.GetOwinContext().Get<ApplicationDbContext>();

    // find the user. I am skipping validations and other checks.
    var userid = User.Identity.GetUserId();
    var user = db.Users.Where(x => x.Id == userid).FirstOrDefault();

    // convert image stream to byte array
    byte[] image = new byte[Profile.ContentLength];
    Profile.InputStream.Read(image, 0, Convert.ToInt32(Profile.ContentLength));

    user.ProfilePicture = image;

    // save changes to database
    db.SaveChanges();

    return RedirectToAction("Index", "Home");
}
[HttpPost]
[ValidateAntiForgeryToken]
公共操作结果配置文件(HttpPostedFileBase配置文件)
{
//获取EF数据库(在您的应用程序中可能采用不同的方式)

var db=HttpContext.GetOwinContext().Get

在这里,我使用ADO.NET实体模型生成了我的DataBaseContext名称,DataBaseContext名称是
ProjectNameEntities
,然后我尝试使用PMC上的以下命令启用迁移
启用迁移-ContextTypeName myProject.Models.ProjectNameEntities
,但随后我在创建DbModelB时遇到以下错误
不支持从先使用数据库或先使用模型创建的DbContext中生成或写入EDMX。EDMX只能从未使用现有DbCompiledModel创建的先使用代码的DbContext中获取。这是否可能与EDMX模型有关?请告诉我控制器是否也可以按您的方式上载图像。您可以使用它使用edmx。区别在于如何将字段添加到模型和生成数据库。控制器和视图部分是相同的。edmx的方法取决于是从edmx生成数据库(模型优先-)还是从数据库创建edmx(数据库优先-)。之后,图像甚至进入数据库也没有显示感谢我发现了问题,但我需要知道如何在登录部分设置图像的大小
[HttpGet]
public ActionResult Profile()
{
    ViewBag.Message = "Update your profile";
    return View();
}
@using (Html.BeginForm("Profile", "Manage", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    <fieldset>
        <legend>Photo</legend>
        <div class="editor-label">
            <label for="profile">FileName:</label>
        </div>
        <div class="editor-field">
             <input name="Profile" id="profile" type="file" />
        </div>
        <p>
        <input type="submit" value="Create" />
        </p>
     </fieldset>
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Profile(HttpPostedFileBase Profile)
{
    // get EF Database (maybe different way in your applicaiton)
    var db = HttpContext.GetOwinContext().Get<ApplicationDbContext>();

    // find the user. I am skipping validations and other checks.
    var userid = User.Identity.GetUserId();
    var user = db.Users.Where(x => x.Id == userid).FirstOrDefault();

    // convert image stream to byte array
    byte[] image = new byte[Profile.ContentLength];
    Profile.InputStream.Read(image, 0, Convert.ToInt32(Profile.ContentLength));

    user.ProfilePicture = image;

    // save changes to database
    db.SaveChanges();

    return RedirectToAction("Index", "Home");
}