How to connect Asp.Nets database with my own context?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2















I'm trying to build an application in Mvc with Individual User Accounts, and I'm trying to connect the automatically generated database with my own context.
The problem I have when I trying to create a view of my Create method I get this errormessage:




There was an error running the selected code generator: 'Unable to
retrieve metadata for 'Projekt_Dejtingsida.Models.Messages' is not
valid. The navigation property 'ApplicationUser' was not found on the
dependent type 'Projekt_Dejtsida.Models.Messages'. The Name value
should be a valid navigation property name.




Please help a beginner out!



Here are the models I'm using:



I have already tried the protected override void OnModelCreating(DbModelBuilder modelBuilder) method, but it didn't work...



public class Messages {
[Key, ForeignKey("ApplicationUser")]
public string Id { get; set; }
[Key]
public int MessageId { get; set; }
public virtual ApplicationUser Sender { get; set; }
public virtual ApplicationUser Receiver { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "You can't send a message without a content")]
[StringLength(300, MinimumLength = 3, ErrorMessage = "Your message should be between 3 and 300 characters")]
public string MessageContent { get; set; }
}

public class FriendRequests {
public string UserId { get; set; }
[Key]
public int RequestId { get; set; }
public virtual ApplicationUser RequestTo { get; set; }
public virtual ApplicationUser RequestFrom { get; set; }
public bool Confirmed { get; set; }
}

public class Profile {
[Key]
public string UserId { get; set; }
public byte ProfilePicture { get; set; }
public string Firstname { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
public string City { get; set; }
public string About { get; set; }
public virtual ICollection<Messages> MyMessages { get; set; }

}


Here is the DbContext:



public class DatesiteContext : DbContext {

public DatesiteContext() : base() { }

public DbSet<FriendRequests> Requests { get; set; }
public DbSet<Messages> Messages { get; set; }
public DbSet<Profile> Profiles { get; set; }
}


Here is the MessageController



[Authorize]
public class MessageController : Controller
{
// GET: Message
public ActionResult Index()
{
var db = new DatesiteContext();
var userId = User.Identity.GetUserId();
var msgs = db.Messages.FirstOrDefault(m => m.Id == userId);

return View(new MessageViewModel {
Sender = msgs.Sender,
Receiver = msgs.Receiver,
MessageContent = msgs.MessageContent
});
}

public ActionResult Create(MessageViewModel model) {
DatesiteContext db = new DatesiteContext();

var userId = User.Identity.GetUserId();
var msgs = db.Messages.FirstOrDefault(m => m.Id == userId);

if (msgs == null) {
db.Messages.Add(new Messages {
Id = userId,
Sender = msgs.Sender,
Receiver = msgs.Receiver,
MessageContent = msgs.MessageContent
});
} else {
msgs.Sender = model.Sender;
msgs.Receiver = model.Receiver;
msgs.MessageContent = model.MessageContent;
}

db.SaveChanges();

return RedirectToAction("Index", "Profile");

}
}









share|improve this question

























  • Where is your ApplicationUser class defined? If your ApplicationUser class is not an EF entity then you cannot use it as a navigation property.

    – ethane
    Jan 3 at 16:28













  • EF goes through your entities and interprets virtual properties as navigation properties to other entities. docs.microsoft.com/en-us/ef/core/modeling/relationships

    – ethane
    Jan 3 at 16:36


















2















I'm trying to build an application in Mvc with Individual User Accounts, and I'm trying to connect the automatically generated database with my own context.
The problem I have when I trying to create a view of my Create method I get this errormessage:




There was an error running the selected code generator: 'Unable to
retrieve metadata for 'Projekt_Dejtingsida.Models.Messages' is not
valid. The navigation property 'ApplicationUser' was not found on the
dependent type 'Projekt_Dejtsida.Models.Messages'. The Name value
should be a valid navigation property name.




Please help a beginner out!



Here are the models I'm using:



I have already tried the protected override void OnModelCreating(DbModelBuilder modelBuilder) method, but it didn't work...



public class Messages {
[Key, ForeignKey("ApplicationUser")]
public string Id { get; set; }
[Key]
public int MessageId { get; set; }
public virtual ApplicationUser Sender { get; set; }
public virtual ApplicationUser Receiver { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "You can't send a message without a content")]
[StringLength(300, MinimumLength = 3, ErrorMessage = "Your message should be between 3 and 300 characters")]
public string MessageContent { get; set; }
}

public class FriendRequests {
public string UserId { get; set; }
[Key]
public int RequestId { get; set; }
public virtual ApplicationUser RequestTo { get; set; }
public virtual ApplicationUser RequestFrom { get; set; }
public bool Confirmed { get; set; }
}

public class Profile {
[Key]
public string UserId { get; set; }
public byte ProfilePicture { get; set; }
public string Firstname { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
public string City { get; set; }
public string About { get; set; }
public virtual ICollection<Messages> MyMessages { get; set; }

}


Here is the DbContext:



public class DatesiteContext : DbContext {

public DatesiteContext() : base() { }

public DbSet<FriendRequests> Requests { get; set; }
public DbSet<Messages> Messages { get; set; }
public DbSet<Profile> Profiles { get; set; }
}


Here is the MessageController



[Authorize]
public class MessageController : Controller
{
// GET: Message
public ActionResult Index()
{
var db = new DatesiteContext();
var userId = User.Identity.GetUserId();
var msgs = db.Messages.FirstOrDefault(m => m.Id == userId);

return View(new MessageViewModel {
Sender = msgs.Sender,
Receiver = msgs.Receiver,
MessageContent = msgs.MessageContent
});
}

public ActionResult Create(MessageViewModel model) {
DatesiteContext db = new DatesiteContext();

var userId = User.Identity.GetUserId();
var msgs = db.Messages.FirstOrDefault(m => m.Id == userId);

if (msgs == null) {
db.Messages.Add(new Messages {
Id = userId,
Sender = msgs.Sender,
Receiver = msgs.Receiver,
MessageContent = msgs.MessageContent
});
} else {
msgs.Sender = model.Sender;
msgs.Receiver = model.Receiver;
msgs.MessageContent = model.MessageContent;
}

db.SaveChanges();

return RedirectToAction("Index", "Profile");

}
}









share|improve this question

























  • Where is your ApplicationUser class defined? If your ApplicationUser class is not an EF entity then you cannot use it as a navigation property.

    – ethane
    Jan 3 at 16:28













  • EF goes through your entities and interprets virtual properties as navigation properties to other entities. docs.microsoft.com/en-us/ef/core/modeling/relationships

    – ethane
    Jan 3 at 16:36














2












2








2








I'm trying to build an application in Mvc with Individual User Accounts, and I'm trying to connect the automatically generated database with my own context.
The problem I have when I trying to create a view of my Create method I get this errormessage:




There was an error running the selected code generator: 'Unable to
retrieve metadata for 'Projekt_Dejtingsida.Models.Messages' is not
valid. The navigation property 'ApplicationUser' was not found on the
dependent type 'Projekt_Dejtsida.Models.Messages'. The Name value
should be a valid navigation property name.




Please help a beginner out!



Here are the models I'm using:



I have already tried the protected override void OnModelCreating(DbModelBuilder modelBuilder) method, but it didn't work...



public class Messages {
[Key, ForeignKey("ApplicationUser")]
public string Id { get; set; }
[Key]
public int MessageId { get; set; }
public virtual ApplicationUser Sender { get; set; }
public virtual ApplicationUser Receiver { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "You can't send a message without a content")]
[StringLength(300, MinimumLength = 3, ErrorMessage = "Your message should be between 3 and 300 characters")]
public string MessageContent { get; set; }
}

public class FriendRequests {
public string UserId { get; set; }
[Key]
public int RequestId { get; set; }
public virtual ApplicationUser RequestTo { get; set; }
public virtual ApplicationUser RequestFrom { get; set; }
public bool Confirmed { get; set; }
}

public class Profile {
[Key]
public string UserId { get; set; }
public byte ProfilePicture { get; set; }
public string Firstname { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
public string City { get; set; }
public string About { get; set; }
public virtual ICollection<Messages> MyMessages { get; set; }

}


Here is the DbContext:



public class DatesiteContext : DbContext {

public DatesiteContext() : base() { }

public DbSet<FriendRequests> Requests { get; set; }
public DbSet<Messages> Messages { get; set; }
public DbSet<Profile> Profiles { get; set; }
}


Here is the MessageController



[Authorize]
public class MessageController : Controller
{
// GET: Message
public ActionResult Index()
{
var db = new DatesiteContext();
var userId = User.Identity.GetUserId();
var msgs = db.Messages.FirstOrDefault(m => m.Id == userId);

return View(new MessageViewModel {
Sender = msgs.Sender,
Receiver = msgs.Receiver,
MessageContent = msgs.MessageContent
});
}

public ActionResult Create(MessageViewModel model) {
DatesiteContext db = new DatesiteContext();

var userId = User.Identity.GetUserId();
var msgs = db.Messages.FirstOrDefault(m => m.Id == userId);

if (msgs == null) {
db.Messages.Add(new Messages {
Id = userId,
Sender = msgs.Sender,
Receiver = msgs.Receiver,
MessageContent = msgs.MessageContent
});
} else {
msgs.Sender = model.Sender;
msgs.Receiver = model.Receiver;
msgs.MessageContent = model.MessageContent;
}

db.SaveChanges();

return RedirectToAction("Index", "Profile");

}
}









share|improve this question
















I'm trying to build an application in Mvc with Individual User Accounts, and I'm trying to connect the automatically generated database with my own context.
The problem I have when I trying to create a view of my Create method I get this errormessage:




There was an error running the selected code generator: 'Unable to
retrieve metadata for 'Projekt_Dejtingsida.Models.Messages' is not
valid. The navigation property 'ApplicationUser' was not found on the
dependent type 'Projekt_Dejtsida.Models.Messages'. The Name value
should be a valid navigation property name.




Please help a beginner out!



Here are the models I'm using:



I have already tried the protected override void OnModelCreating(DbModelBuilder modelBuilder) method, but it didn't work...



public class Messages {
[Key, ForeignKey("ApplicationUser")]
public string Id { get; set; }
[Key]
public int MessageId { get; set; }
public virtual ApplicationUser Sender { get; set; }
public virtual ApplicationUser Receiver { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "You can't send a message without a content")]
[StringLength(300, MinimumLength = 3, ErrorMessage = "Your message should be between 3 and 300 characters")]
public string MessageContent { get; set; }
}

public class FriendRequests {
public string UserId { get; set; }
[Key]
public int RequestId { get; set; }
public virtual ApplicationUser RequestTo { get; set; }
public virtual ApplicationUser RequestFrom { get; set; }
public bool Confirmed { get; set; }
}

public class Profile {
[Key]
public string UserId { get; set; }
public byte ProfilePicture { get; set; }
public string Firstname { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
public string City { get; set; }
public string About { get; set; }
public virtual ICollection<Messages> MyMessages { get; set; }

}


Here is the DbContext:



public class DatesiteContext : DbContext {

public DatesiteContext() : base() { }

public DbSet<FriendRequests> Requests { get; set; }
public DbSet<Messages> Messages { get; set; }
public DbSet<Profile> Profiles { get; set; }
}


Here is the MessageController



[Authorize]
public class MessageController : Controller
{
// GET: Message
public ActionResult Index()
{
var db = new DatesiteContext();
var userId = User.Identity.GetUserId();
var msgs = db.Messages.FirstOrDefault(m => m.Id == userId);

return View(new MessageViewModel {
Sender = msgs.Sender,
Receiver = msgs.Receiver,
MessageContent = msgs.MessageContent
});
}

public ActionResult Create(MessageViewModel model) {
DatesiteContext db = new DatesiteContext();

var userId = User.Identity.GetUserId();
var msgs = db.Messages.FirstOrDefault(m => m.Id == userId);

if (msgs == null) {
db.Messages.Add(new Messages {
Id = userId,
Sender = msgs.Sender,
Receiver = msgs.Receiver,
MessageContent = msgs.MessageContent
});
} else {
msgs.Sender = model.Sender;
msgs.Receiver = model.Receiver;
msgs.MessageContent = model.MessageContent;
}

db.SaveChanges();

return RedirectToAction("Index", "Profile");

}
}






c# .net database dbcontext






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 15:23









Amy

22k1876133




22k1876133










asked Jan 3 at 12:57









Yasmin KamilYasmin Kamil

161




161













  • Where is your ApplicationUser class defined? If your ApplicationUser class is not an EF entity then you cannot use it as a navigation property.

    – ethane
    Jan 3 at 16:28













  • EF goes through your entities and interprets virtual properties as navigation properties to other entities. docs.microsoft.com/en-us/ef/core/modeling/relationships

    – ethane
    Jan 3 at 16:36



















  • Where is your ApplicationUser class defined? If your ApplicationUser class is not an EF entity then you cannot use it as a navigation property.

    – ethane
    Jan 3 at 16:28













  • EF goes through your entities and interprets virtual properties as navigation properties to other entities. docs.microsoft.com/en-us/ef/core/modeling/relationships

    – ethane
    Jan 3 at 16:36

















Where is your ApplicationUser class defined? If your ApplicationUser class is not an EF entity then you cannot use it as a navigation property.

– ethane
Jan 3 at 16:28







Where is your ApplicationUser class defined? If your ApplicationUser class is not an EF entity then you cannot use it as a navigation property.

– ethane
Jan 3 at 16:28















EF goes through your entities and interprets virtual properties as navigation properties to other entities. docs.microsoft.com/en-us/ef/core/modeling/relationships

– ethane
Jan 3 at 16:36





EF goes through your entities and interprets virtual properties as navigation properties to other entities. docs.microsoft.com/en-us/ef/core/modeling/relationships

– ethane
Jan 3 at 16:36












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54022768%2fhow-to-connect-asp-nets-database-with-my-own-context%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54022768%2fhow-to-connect-asp-nets-database-with-my-own-context%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules