Solved

How create this view in sql server?

Posted on 2015-01-14
4
69 Views
Last Modified: 2015-01-16
I need to create an view in sql server. I do this easily but this one is a little bit more complicated than I can do.
I have two tables.  

   [Table("PERSONS")]
    public partial class PERSON
    {
        public PERSON()
        {
            Movs = new HashSet<Mov>();
            Movs1 = new HashSet<Mov>();
        }

        public int PersonID { get; set; }

        [Required]
        [StringLength(50)]
        public string PersonName { get; set; }

        public virtual ICollection<Mov> Movs { get; set; }

        public virtual ICollection<Mov> Movs1 { get; set; }
    }

    public partial class Mov
    {
        public int MovID { get; set; }

        public int PersonID { get; set; }

        public int OtherPersonID { get; set; }

        public virtual PERSON PERSON { get; set; }

        public virtual PERSON PERSON1 { get; set; }
    }

and the context is:

 public partial class Model1 : DbContext
    {
        public Model1()
            : base("name=Model1")
        {
        }

        public virtual DbSet<Mov> Movs { get; set; }
        public virtual DbSet<PERSON> PERSONS { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<PERSON>()
                .Property(e => e.PersonName)
                .IsUnicode(false);

            modelBuilder.Entity<PERSON>()
                .HasMany(e => e.Movs)
                .WithRequired(e => e.PERSON)
                .HasForeignKey(e => e.PersonID)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<PERSON>()
                .HasMany(e => e.Movs1)
                .WithRequired(e => e.PERSON1)
                .HasForeignKey(e => e.OtherPersonID)
                .WillCascadeOnDelete(false);
        }
    }



i need a view with both names.

how could I do that?
0
Comment
Question by:Deraldo Silva
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 11

Expert Comment

by:Ray
ID: 40550194
If you get no other response, you should really consider adding this to one of the programming topics.  Most people in this topic (SQL) don't tend to speak sql in terms of developer coding (a few do though).
0
 

Author Comment

by:Deraldo Silva
ID: 40550222
Ok.
i have this relation in the db.
i need a view with both names.

thx for your attention
relation.png
0
 
LVL 9

Accepted Solution

by:
Valliappan AN earned 500 total points
ID: 40554067
CREATE VIEW MovsView
AS
SELECT MovID, P.PersonName, OP.PersonName AS OtherPersonName
FROM Movs M
JOIN Persons P ON M.PersonID = P.PersonID
JOIN Persons OP ON M.OtherPersonID = OP.PersonID

Note: You may use left join, if personids can be NULL in Movs table.

HTH.
0
 

Author Closing Comment

by:Deraldo Silva
ID: 40554124
Thx
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question