Solved

How create this view in sql server?

Posted on 2015-01-14
4
67 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
  • 2
4 Comments
 
LVL 10

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Upgrading to SQL Server 2015 Express 2 34
Need help with a query 14 39
SQL Instance service gone? 5 38
Need multiple Group By's 8 26
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

749 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